sbay
sbay

Reputation: 445

Regex: Match everything between characters or new line

I am trying to come up with a regex that would identify instances of abc=123456|123456 from the following snippet:

xyz=abcdef|abcdef||abc=123456|123456||cat=dog|dog||foo=bar|bar||
xyz=abcdef|abcdef||abc=123456|123456
xyz=abcdef|abcdef||abc=123456|123456||
abc=123456|123456||xyz=abcdef|abcdef||

The requirement here is that the match string can have a trailing double pipe or it could not have it.

I am currently using this:

/abc=(.*?)+((?=\|\|)|(?=\r|\n))/

But this seems to break with the OR condition for end of match.

Appreciate any help in advance.

Preview link: http://regexr.com/3be2t

Upvotes: 1

Views: 2649

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

You may use end of the line anchor instead of \r or \n and you have to remove the + which exists just after to (.*?). With +, it does a greedy match and the capturing group must contain an empty string.

\babc=(.*?)(?=\|\||$)

or

\babc=(.*?)(?=\|\||\r|\n)

DEMO

Upvotes: 1

Related Questions