Author Topic: Regex Match function  (Read 1207 times)

Ninja

  • Newbie
  • *
  • Posts: 8
Regex Match function
« on: November 14, 2021, 07:13:05 PM »
Looking through the documentation, it seems VA only has a Regex Replace function.
Is there a way to have Regex Match in VA to retrieve parts of a text variable?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Regex Match function
« Reply #1 on: November 14, 2021, 07:17:51 PM »
If the "{TXTREGEXREPLACE:}" token does not suit your purposes, you'll want to look into using an inline function.

Ninja

  • Newbie
  • *
  • Posts: 8
Re: Regex Match function
« Reply #2 on: November 14, 2021, 09:06:58 PM »
From my understanding, {TXTREGEXREPLACE:} seems to return the replacement text, not the part of the source that matches the specified pattern.

From search results I found a Music Jukebox example that uses inline Regex Match function. However since I'm new to both C# and inline functions, it's a bit over my head

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 280
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Regex Match function
« Reply #3 on: November 16, 2021, 02:37:01 PM »
Here's an example to get you started, including links for more info.  I write in VB.net, but C# is quite similar but for some methods and some syntax difference:

Code: (VB.net) [Select]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks
Imports System.Text.RegularExpressions

'===Regex Example inline in VB.net (C# pretty much the same, minor syntax differences)===

Public Class VAInline
'dim inputVar as string = "example no match text"
'--Comment out the line below and uncomment line above to test non-match--
  dim inputVar as string = "192.168.0.1"
dim patternVar as string = "\d*\.\d*\.\d*\.\d*"

Public Sub Main()

dim match as Match = Regex.Match(inputVar, patternVar, RegexOptions.IgnoreCase)
if (match.Success)
'Do something here when match found
VA.WriteToLog("MATCH FOUND!", "green")
else
'Do something here when no match found (optional)
VA.WriteToLog("NO match found...", "red")
end if

End Sub

End Class

RegexMatch info can be found here:
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=net-5.0


Further regex examples in VB.net on DotNetPearls:
https://www.dotnetperls.com/regex-vbnet


RegexOptions (3rd argument shown being used above as 'IgnoreCase') can be found here:
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions?view=net-5.0

Ninja

  • Newbie
  • *
  • Posts: 8
Re: Regex Match function
« Reply #4 on: November 17, 2021, 05:16:19 PM »
Thanks a lot SemlerPDX! The code is working. I'll figure out how it works, and how to modify it :D

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 280
  • Upstanding Lunatic
    • My AVCS Homepage
Re: Regex Match function
« Reply #5 on: November 18, 2021, 01:23:50 PM »
Best wishes and good luck!  FTR, you can also use the 'IsMatch' method for a more straightforward command:

Code: (VB.net) [Select]
...
Imports System.Text.RegularExpressions

Public Class VAInline
  dim inputVar as string = "192.168.0.1"
dim patternVar as string = "\d*\.\d*\.\d*\.\d*"

Public Sub Main()
if (Regex.IsMatch(inputVar, patternVar))
VA.WriteToLog("MATCH FOUND!", "green")
else
VA.WriteToLog("NO match found...", "red")
end if
End Sub

End Class

RegexIsMatch info can be found here:
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch?view=net-5.0#System_Text_RegularExpressions_Regex_IsMatch_System_String_System_String_
« Last Edit: November 18, 2021, 03:13:58 PM by SemlerPDX »

Ninja

  • Newbie
  • *
  • Posts: 8
Re: Regex Match function
« Reply #6 on: November 19, 2021, 07:23:34 AM »
Hey, thanks a ton for that! It's much simpler to understand. Figured out how to check if it a string matches a pattern, and return the result to VA :D