How to extract a value between a dynamic variable and a fixed string?

Hello,

I need to extract a value between a dynamic value from a datatable and a fixed string.

I already searched a solution and I tried to use the following expression :
Regex.Match(inputStr, “(?<=Term1)([\S\s]*)(?=Term2)”).Value

The issue is that Term1 in my case is read from a datatable and it changes everytime and that Regex expression doesn’t seem to work.

Iniatially I tried to change Term1 with that row(“Mycolumn”).tostring but without any luck.

Any ideas ?

Thanks in advance !

Hi,

Can you try Regex.Escape as the following?

System.Text.RegularExpressions.Regex.Match(inputStr, "(?<="+System.Text.RegularExpressions.Regex.Escape(row("Mycolumn").ToString)+")([\S\s]*)(?=Term2)").Value

Regards,

Can you share the sample of input and expected output so easy to write regex

@darie.leolea

It works. Thank you !

1 Like

How should I change the expression in case that I would have to search between 2 rows and not between a row and a fixed string ? Thanks in advance !

Hi,

How about the following? This will match chracters b/w MyColumn1 and MyColumn2.

System.Text.RegularExpressions.Regex.Match(myVar, "(?<="+System.Text.RegularExpressions.Regex.Escape(row("Mycolumn1").ToString)+")([\S\s]*)(?="+System.Text.RegularExpressions.Regex.Escape(row("Mycolumn2").ToString)+")").Value

Regards,

1 Like

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