How to convert matched Regex patterns to String?


#1

I am able to extract the amount value from a string and store it in a variable of type System.Collections.Generic.IEnumerable (of System.Text.RegularExpressionsMatch).

Now I need to display this matched pattern in a message box, for which I need to convert the matched pattern to string. How can I achieve it ?


#2

Hello,

I’m not sure it will help you for what you want but…

I wrote some testable code activity for it that you can find attached
I was basically using a list of key value pair of String and Regex to then retrieve the pattern matched

here is some input to test:

ABCD 123456789012
1234578901
Hello

In case you can’t open the workflow, I’m pasting the code under.

Dim patterns As New List(Of String) From {"\bHello\b","\d{10,12}","[A-Z]{4}"}

Dim kvpList As New List(Of KeyValuePair(Of String,System.Text.RegularExpressions.Regex))

patterns.ForEach(Sub§ kvpList.Add(New KeyValuePair(Of String,Regex)(p,New Regex§)))

Dim ib As String = InputBox((“Enter a string to compare to your pattern” & vbNewLine &
“The first pattern matched will be prompted”))

If Not String.IsNullOrWhiteSpace(ib)

Dim matches As List(Of KeyValuePair(Of String,System.Text.RegularExpressions.Regex)) = kvpList.Where(Function(kvp) kvp.Value.IsMatch(ib)).ToList()


Dim result As String = String.Empty
			  
matches.ForEach(Sub (m) result = result & vbNewLine & m.Key)

msgbox(result.TrimEnd)

End If

if you need some explanations on how it work I can make some comments on it later

Main.xaml (5.7 KB)
project.json (264 Bytes)

Good luck :slight_smile:


#3

I could do it, using variable(0).value in the message box