Search code examples
regexuipath

Regular Expression for New line is not working in UiPath


I have the following text in a text file, and I wanted to read the 2nd line using regexp in UiPath, a company name (i.e. SOME ANALYTICS).

INVOICE DETAILS Invoice To:
SOME ANALYTICS
Invoice# : R0264
Invoice Date: 27/06/20

I have tried following regular expressions on https://regexr.com/ to retrieve the data and it is working fine. But the same expression is not working on UiPath.

(?<=To:[\n\r]).+


Solution

  • You can use

    (?<=^.*\n).+
    

    Details:

    • (?<=^.*\n) - a positive lookbehind that matches a position that occurs right after the first line that ends with an LF char
    • .+ - one or more chars other than an LF char (the second line).

    See the regex demo.

    Another option is to take out the CR out of the equation and make the regex multiline flag insensitive by replacing ^ with \A:

    (?<=\A[^\r\n]*\r?\n)[^\r\n]+
    

    This regex matches

    • (?<=\A[^\r\n]*\r?\n) - a location that is immediately preceded with
      • \A - start of string
      • [^\r\n]* - zero or more chars other than CR and LF as many as possible
      • \r?\n - an optional CR and then LF char
    • [^\r\n]+ - one or more chars other than LF and CR.

    See this regex demo.