Search code examples
phpregexpreg-matchregex-lookarounds

REGEX - String must not start from these words and may contain alphanumeric characters


Requirement are as follows:

1.First three characters must not be "CP ","C P". Note there is a space in both these words
2.First character may be alphanumeric, including À, Â, Ç, È
3.2nd and 3rd caracter can be any ascii character other than (0-31)

The part that I am having trouble with is how can I write regex where the string does not start with "CP " word or "C P" that word. Note that ASCII 32 is space character which is valid in regex match but not but in combination with "CP ".

I have tried something like this as starting point but to no avail

(?!CP )[^\x00-\x1F]

Sample Strings

invalid text : CP 420, C P 420

Valid text: abc 420, 420 CP etc.


Solution

  • You need a starting anchor, you need to add the optional whitespace into the negative lookahead. It will be easier to list the characters you want than to list the characters you don't, since it is 2 different ranges. Try:

    ^(?!(?:C ?P|CP ?))[A-Za-z\dÀÂÇÈ][\x20-\xFF]{1,2}
    

    Demo: https://regex101.com/r/4Rhv2V/2/