Search code examples
regexregex-lookaroundsregex-group

Regular expression - Conditional Lookbehind, filter start?


I'm trying to do some checks on a string where I use a regular expression using a lookbehind and lookahead to retrieve a certain part of a piece of text. The typical format of the text is as below:

BS-123456.v2019

The portion of the text that I retrieve is 123456 using the following regex:

(?<=\-)(.*?)(?=\.v)

Basically I always want to retrieve the portion of the text between the hyphen '-' and the '.v' part.

This is working nicely.

I need to however make an adjustment to filter out any thing that starts with the word 'Copy', ignoring case. So for instance if I have the following text:

Copy_BS-123456.v2019

or Copy-BS-123456.v2019

or Copy BS-123456.v2019

and so forth the regex must return nothing.

I tried something along the lines of

^(?!Copy)(?<=\-)(.*?)(?=\.v)

but it doesn't seem to work. I'm relatively new to regex so please excuse me if I'm missing something obvious. Any suggestions or a point in the right direction would be appreciated.


Solution

  • This part (?!Copy) of the pattern will succeed, but using the positive lookbehind (?<=-) directly after it will not get a match because the positive lookahead will assert directly after the start of the string that what is on the left should be a -

    You could use a capturing group instead:

    ^(?!Copy).*?-(\d{6})\.v
    

    Regex demo

    If there is only a single - before the number, you could also use a negated character class [^ matching any char except - or a newline.

    ^(?!Copy)[^-\r\n]*-(\d{6})\.v
    

    Regex demo

    Note that in your initial pattern, you can omit the capturing group to get a match only if you use the lookarounds (?<=\-).*?(?=\.v)