Regex matches with contents of an array or list

Greetings,

A fairly straight-forward question here:

Say I have the following regex script:

System.Text.RegularExpressions.Regex.Match(QuoteTxt,"\d+\d{1,3}").Value

How would I make it so that it also does a regex match for the contents of an array such as: {“blue”, “red”, “green”}

I found the following solution on the forum:

(From r In dtData.AsEnumerable
Where variableList.Any(Function (x) r(0).toString.Contains(x))
Select r).CopyToDataTable

But I would prefer to do it the regex.match way

How would I do that?

Many thanks

Hi,

How about the following?

arrStr = {"blue","red","green"}

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",arrStr)).Value

if the array contains regex special character and need to escape it, the following will work.

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",arrStr.Select(Function(s) System.Text.RegularExpressions.Regex.Escape(s)))).Value

Regards,

Yoichi,

Thank you kindly, sir

So, would I be able to do something like:

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",arrStr|"\d+\d{1,3}")).Value

or

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",arrStr) and "\d+\d{1,3}")).Value

I am trying to do a Regex match for the array contents plus the digits expression.

If there is another way to do it, perhaps with filtering datatable, I am open to all options.

Really appreciate your help!

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",arrStr.Concat({ "\d+\d{1,3}"}))).Value

PS i think \d+\d{1,3} is same as \d+

Regards,

Hi Yoichi,

I can see what your solution does, and I thank you for sharing. Let me explain what I am trying to accomplish a little more in depth:

So, This is a regex match done via the expression " ^.* ", to grab it line by line.

Now, I want to do a regex match for the city names circled in red, and all of the pricing detail (example circled in blue), whiie keeping the line by line format (so I can put into a datatable - which I have already accomplished successfully via a while loop and a counter variable)

So, I know, in order to use multiple regex expressions, you can use the “|” operator between each expression, but in this case, I am trying to use the String.Join(“|”, array) results (which are the city names) PLUS the regex for the pricing.

Was also thinking about saving the intital regex capture (the screenshot above) to a datetable, and then filtering that datatable to keep rows that contain the contents of an array (city names) and the regex for the pricing information. If you have a LINQ/VB.Net script to tackle it that way, I would be open to that as well.

I have seen this code on the forum for filtering datatable based on contents in an array:

(From r in yourDataTableVar.AsEnumerable
Where yourFilterTokenArrayVar.Any(Function (x) r(“columnName”).toString.Contains(x))
Select r).CopyToDataTable

But how could I also add in the regex expression for the pricing as well?

Hope this makes sense! Thank you again!

Hi,

How about the following?

(From r In yourDataTableVar.AsEnumerable
Where yourFilterTokenArrayVar.Any(Function (x) System.Text.RegularExpressions.Regex.IsMatch(r("columnName").ToString,x))
Select r).CopyToDataTable

OR

(From r In yourDataTableVar.AsEnumerable
Where System.Text.RegularExpressions.Regex.IsMatch(r("columnName").ToString,String.Join("|",yourFilterTokenArrayVar))
Select r).CopyToDataTable

note : yourFilterTokenArrayVar ={“Califorinia”,“Nevada”,“\d+”} for example.

Regards,

Yoichi,

Thank you so much for the responses. After some experimentation, it looks like I was looking for the “&” operator:

System.Text.RegularExpressions.Regex.Match(QuoteTxt,String.Join("|",testArray)).Value & System.Text.RegularExpressions.Regex.Match(QuoteTxt, "\d{1,3}").Value

Thank you again for your responses!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.