Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression which does not match at a certain capitalization

I'm looking for a regular expression (in JS, but I don't think that it matters) which matches all upper case/lower case possibilities of a certain string but one.

Example: Let my string be "aBc". Then my regular expression should match like the following

var regex=/ABC|ABc|AbC|Abc|aBC|abC|abc/g;

In this case it is possible to spell it out but in general a string with n word characters allows 2^n-1 strings to match. For big n this is not well scaled anymore. Therefore I look for another way.

Edit: Since many of you don't seem to get my point: The regex must NOT match "aBc". So it's like /abc/i but not /aBc/.

For simplicity you can check if you expression does what it is supposed to do here: http://regex101.com/r/kP5aX2

Your regex should match exactly like mine.

like image 560
principal-ideal-domain Avatar asked Mar 06 '26 14:03

principal-ideal-domain


1 Answers

Try

var regex = /(?!aBc)[a-cA-C]{3}/g;

(?!aBc) is a so called negative lookahead, which should do the trick you desire..

UPDATE:

to refer to the comments...

var regex = /((?![a-zA-Z]*aBc[a-zA-Z]*)[a-zA-Z]*[aA][bB][cC][a-zA-Z]*)/g;

UPDATE 2

if you dont want to match the whole words, but only the 3-character -trings, you should do it like that

var regex = /(?!aBc)[aA][bB][cC]/g; 

see http://regex101.com/r/jQ3hS8

like image 108
nozzleman Avatar answered Mar 09 '26 03:03

nozzleman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!