Search code examples
regexlookbehind

whitespaces inside positive lookbehind regex


wont work, how to fix this problem? I need to get the number. Foo can bee with space or without space.

(?<=Foo:\s?</b>).+\d
<b>Foo:</b>2013<br><b>Bar:</b>
<b>Foo: </b>2013<br><b>Bar:</b>

Solution

  • The problem is that variable length lookbehind are not allowed in many languages, however some languages allow to put alternatives inside:

    If you don't use Python or javascript, try this:

    (?<=Foo:\s</b>|Foo:</b>).+\d
    

    If you use Perl or PHP you can do this too:

    Foo:\s?</b>\K.+\d
    

    (but the difference in this second pattern is that Foo:\s?</b> are matched and remove from the match result. Thus if you need to match these characters before you can't use this way)