Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match capital letters and small letters by RegExp

I wrote some RegExp pattren like this :

SomeText

But I want the pattren to match with :

Sometext
sOMeTeXt
SOMETEXT
SoMEteXt

Somethings like that !

In fact I want to use this

\s?[^a-zA-Z0-9\_]SomeText[^a-zA-Z0-9\_]

what should i do ?

like image 959
Ahmad Alshaikh Avatar asked Oct 03 '10 18:10

Ahmad Alshaikh


1 Answers

In many regex implementations, you can specify modifiers that apply to a given part of your pattern. Case-insensitivity is one of those modifiers:

\s?[^a-zA-Z0-9\_](?i)sometext(?-i)[^a-zA-Z0-9\_]

The section between (?i) and (?-i) will be put into case-insensitive mode. According to this comparison table, this is supported if you're using .net, Java, Perl, PCRE, Ruby or the JGsoft engine.

Of course, since you're specifying both a-z and A-Z in your character classes, you could simplify and use the case-insensitive modifier on the entire pattern:

/\s?[^a-z0-9\_]sometext[^a-z0-9\_]/i
like image 90
Daniel Vandersluis Avatar answered Sep 24 '22 03:09

Daniel Vandersluis