Calico Kitty
Calico Kitty

Reputation: 1

Regular expression

Below is my text Data from which I have to find MI, mi, MumbaiIndians, MUMBAIINDIANS words through regex.

HATHWAY / MBBS : [PM1-PTY-PM / MUMBAIINDIANS0/2/2] || WICKET DOWN 
HATHWAY / MBBS : [PM1-PTY-PM /MUMBAIINDIANS0/2/2] || WICKET DOWN
HATHWAY / MBBS : [PM1-PTY-PM / MI0/2/2] || WICKET DOWN
HATHWAY / MBBS : [PM1-PTY-PM /MI0/2/2] || WICKET DOWN
HATHWAY / MBBS : [PM1-PTY-PM /MI ] || WICKET DOWN
HATHWAY / MBBS : [PM1-PTY-PM / mi0/2/2] || WICKET DOWN 
HATHWAY / MBBS : [PM1-PTY-PM / MumbaiINDIANS0/2/2] || WICKET DOWN
HATHWAY / MBBS : [PM1-PTY-PM /Me ] || WICKET DOWN
 

I wrote regex query for this as below

`/((\bMi[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*)|(MI[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*)|(\bMi[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*)|(MumbaiINDIANS[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*)|(\bMi[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*)|(mi[^0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*))/`

It is working but It is not efficient and looks very complex ... what I want is MI, mi, me, MumbaiIndians , MUMBAIINDIANS words to words to gets selected irrespective of their locations.

Please anyone tell me how to optimize this query.

Upvotes: 0

Views: 76

Answers (2)

Roko C. Buljan
Roko C. Buljan

Reputation: 206593

You could use the ig for insensitive, global with a positive lookbehind / lookahead:

/(?<=[\d \/])mumbaiindians|mi|me(?=[\d \/])/ig

Regex101.com demo

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163632

Seems like you might shorten your pattern to (using a case insensitive match)

To prevent a partial match, you could use an assertion to make sure that it is not followed by a char a-zA-Z using a negative lookahead (?!

(?i)\b(?:mumbaiindians|mi)(?![A-Za-z])

Regex demo

Or you can assert one of the listed chars to the right using a positive lookahead (?=

(?i)\b(?:mumbaiindians|mi)(?=[0-9-!\s$%^&*()_+|~=`{}\[\]:\";'<>?,.\/])

Regex demo

Upvotes: 1

Related Questions