Fetch information with regex

Hi!

I have a String which looks like this:

some text
some text variableName

I want to fetch the bold text between the line break on the first row and a previously identified variable on the second row. The first row of text differs from time to time, that’s why I want to use the line break as a seperator.

How can I code this with regex? I know that you have to do something like the example below but how do you handle line breaks in such an expression? Can you use /n?

System.Text.RegularExpressions.Regex.Match(text,“((?<=“theLineBreak”).*(?=, “+variableName+”))”).Value

Best regards
Klara

Try this
System.Text.RegularExpressions.Regex.Match(text,“(.*)(?= variableName)”).Value

@Karthick_Settu
Thanks for your response.
The .* means that the first row always have to end with a dot right?
That is the problem, sometimes it’s “.” and sometime it’s “:” “,” or nothing at all.
That is why I am wondering about the line break separator.

Thanks!

Actually dot matches with any character and * matches with zero and unlimited times. Did you give a try on this regex ? Cause it is working for me

@Karthick_Settu

Thank you, I was not aware of that. The line you provided worked for one of the cases, but when I put the variable name within + operator it worked for 3 out of 4 cases.

Like this:
System.Text.RegularExpressions.Regex.Match(text,“(.).(?= “+variableName+”)”).Value

The case where it didn’t work was when the String looked like this:

"Some text,

Some text.
Some text variableName"

The output in this case became the text in the first line instead of the bold text.

1 Like

Try this
System.Text.RegularExpressions.Regex.Match(text,“(\b…*\b)(?=.? “+variableName+”)”).Value

@Karthick_Settu doesn’t work either, zero output on all of the four cases.

Between \b and *\b it’s only two dots. Can you check once again

This suggestion was great! I realised that I had misplaced another activity in a different part of my project. But now this line with ".* " is working. Thank you so much for your effort in the matter!

Best regards Klara

1 Like

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