Search code examples
regexregex-lookaroundsregex-look-ahead

Stop at first lookahead match with two options


I'm trying to generate a RegEx that grabs all the content in between "<" and ">" and stops if it finds a "|". For example:

This is the link <stackoverlflow.com>
This is the link <https://stackoverlflow.com>
This is the link <stackoverlflow.com | Click here>
This is the link <https://stackoverlflow.com | Click here>

I tried with something like this but it doesn't work: ((?!<)[^\s]+((?=>)|(?=\|)))
Can you help me here?


Solution

  • You may use

    (?<=<)[^<>\s|]+
    

    See the regex demo

    Details

    • (?<=<) - a location immediately preceded with <
    • [^<>\s|]+ - 1 or more chars other than <, >, whitespace and |.