How to search words which not contain the search pattern?

Find, replace, find in files, replace in files, regular expressions

How to search words which not contain the search pattern?

Postby Sergeant » Wed Mar 12, 2008 5:17 am

Hello,

I have a file containing following lines:

AbcTempSpeed
XyzTempSpeed
_XyzSpeed
_ZeroTempSpeed
AbcSpeed
MedianTemSpeed
_AbcTempPressure

I need to find all words which do not include the pattern 'Temp'
('_XyzSpeed' , 'AbcSpeed' and 'MedianTemSpeed' in the above example).

Is this possible using UE regular expressions?

Thanks for any hint.
Regards
Sergeant
Sergeant
Newbie
 
Posts: 1
Joined: Wed Mar 12, 2008 4:39 am

Re: How to search words which not contain the search pattern?

Postby pietzcker » Wed Mar 12, 2008 6:30 am

Hi Sergeant,

it is possible if you have UE V12 or higher (which you didn't tell us although the Readme forum topic is quite explicit about this).

The following Perl style regex will match a word that doesn't contain the string Temp:

Code: Select all
\b(?:(?!Temp)\w)+\b

(courtesy of RegexBuddy)

HTH,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: How to search words which not contain the search pattern?

Postby mjcarman » Wed Mar 12, 2008 12:33 pm

That's clever, Tim. At first I thought that the (?:) was superfluous but I see now that you're using it to apply the negative look-ahead before each character.
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: How to search words which not contain the search pattern?

Postby pietzcker » Wed Mar 12, 2008 3:39 pm

I'd love to take credit for it, but I grokked it from RegexBuddy. :)
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: How to search words which not contain the search pattern?

Postby Jane » Wed Mar 19, 2008 11:11 pm

I could be wrong, but I think the ?: just creates a non-capturing group. It will work without it, but you'll waste resources saving the capture. The iteration is caused by having the + outside the parenthesis rather than inside. If you put it inside
i.e.\b(?:(?!Temp)\w+)\b
you will only avoid words that start with Temp.

Nice job Tim,
Jane
User avatar
Jane
Basic User
Basic User
 
Posts: 22
Joined: Sat Aug 05, 2006 11:00 pm
Location: Canada

Re: How to search words which not contain the search pattern?

Postby mjcarman » Thu Mar 20, 2008 8:35 am

Jane, You are correct. The ?: makes the grouping a non-capturing one. The grouping is the important thing, not the (lack of) capture.
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am


Return to Find/Replace/Regular Expressions