Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex dilemma

Status
Not open for further replies.

AO940

IS-IT--Management
Joined
Jun 16, 2006
Messages
2
Location
FI
I've read regex tutorials but still have not found the answer to this question.

How do you do a prel-style negation so that a search result is omitted if a specified pattern of characters (string) is found *anywhere* in a list of words? Example: Find exp1 but skip if exp2 is also found in the same string.

[KEYWORDS:]
exp1something /*found, exp1 found, but not exp2
exp1somethingexp2 /*skipped, exp1 found but exp2 also found
someexp1thing /*found
somethingexp1 /*found
exp2exp1 /skipped, because the string exp2 was found

Could it be just (!exp2)
 
You could do it with a negative look-behind and look-ahead, but why?

I agree with Trojan, it makes better sense to use two regexs.
 
Code:
my @strings=('exp1something','exp1somethingexp2','someexp1thing','somethingexp1','exp2exp1');

for (@strings) {
  print "$_\n" if !/exp2/ and /exp1/;
}

Due to the 'and' operator shortcutting, /exp1/ won't even be evaluated unless it can't find exp2.

Trojan's on the money, as usual. Using two regexes 'reads' better and makes sense when you come to look at it later.

 
Thanx for the info. But as far as I know, strings appear in parentheses. So, instead of writing !/exp2/, is it also possible to write the same expression as !(exp2)?
 
Simple answer: no.

!(exp2)? looks for a literal exclamation mark followed by the optional string `exp2'. brigmar's solution is what you need. Doing all this in a single regexp will be an unmaintainable nightmare, even if you managed to write one.
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
350
  • Locked
  • Question Question
Replies
4
Views
472
  • Locked
  • Question Question
Replies
1
Views
309
  • Locked
  • Question Question
Replies
5
Views
452

Part and Inventory Search

Sponsor

Back
Top