Unable to use Regex over multiple lines


I am unable to get a result from the following regex (System.Text.RegularExpressions.Regex.Match(PdfText, “Ans1\s:([^?.])Q2”).Groups(1).Value.Trim).

The value I am after is the phone number from the string below:

Q1 :- Phone number? Ans1
Q2 :-

Any ideas what is going wrong please? The regex displays correctly when typed into regex101.com but once it is in UiPath it doesn’t. Is this something to do with multiple lines? The problem is that I can’t use \n as the value may appear on the same line as Ans1 in some instances.

Any help would be much appreciated.



Try using the any of the below patterns.

Numbers hard coded: (?<=Ans1\s:).*(?=\sQ2)
Variable numbers: (?<=Ans)\d+\s:(.*)(?=\sQ\d)

Assign the below expression to a string variable. Numbers are hard coded in the pattern.

System.Text.RegularExpressions.Regex.Match(inputString, "(?<=Ans1\s:).*(?=\sQ2)").Value

Below expression is more generic without hard coding the numbers.

System.Text.RegularExpressions.Regex.Match(inputString, "(?<=Ans)\d+\s:(.*)(?=\sQ\d)").Groups(1).Value

1 Like


The patterns above worked just fine in the regex tester (https://www.regextester.com/) but once in UiPath the results returned an empty value. Is it something to do with the value being across multiple lines?

1 Like


Got it working using this pattern.



1 Like

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