Search code examples
regexpowershellregex-lookaroundsregex-groupregex-greedy

RegEx for matching specific words and ignoring new lines


Full Text =

"
......
A= 
B= 12345 
....."

I want to get empty word "" between A= and line break. and want to get "12345" between B= and line break.

How can I get words using regular expression?

(?<=A=)\s*(\S*)\s* 

or

(?<=B=)\s*(\S*)\s* 

But, it also brought the next line contents.


Solution

  • How about this pattern:

    (?<=[A-Z]=)[ ]*(\S*)
    

    This pattern avoids the problem of wrapping over to the next line by first only allowing spaces after the A= (or B= etc.). This means that in the case of the A= line, which only has a newline character after it, the [ ]* would match zero times. Second, for the content it only uses (\S*), which also will not consume whitespace and wrap to the next line.

    Demo