TheTechGuy
TheTechGuy

Reputation: 17384

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.

Upvotes: 0

Views: 247

Answers (2)

Rhushida Kashalkar
Rhushida Kashalkar

Reputation: 11

I tried with some REGEX expressions, and arrived at this.

^((?!cp )(?!c p))(.)*$

This will yield following results

acp  -> Matched
cpa  -> Matched
fsdgsfdgsfd  -> Matched
c psadsadsadsa  -> Not Matched
cpasdsa  -> Matched
ac pasdsadsa  -> Matched
cp sadfsdfsdf  -> Not Matched
Ècp sadfsdfsdf  -> Matched

Not sure if this is what you will require. If not, please give more examples and I'll dig deeper.

Edit 1: As per refined requirements, try if this works.

^((?!CP )(?!C P)(?!.[\x00-\x1F][\x00-\x1F]))(.)*$

Upvotes: 0

chris85
chris85

Reputation: 23880

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/

Upvotes: 1

Related Questions