Re: regex for any word from a set of words
"Uttormark, Tim" <[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <9FF69050257FC0479018211CD530F8BC0C43E566@CORP2K3MAIL.corp.peopleclick.com> |
I presume the problem is to define a regular expression which returns
the entire content of a string for all strings not containing any of a
set of string literals.
How about this:
^((?:(?!foo)(?!bar)(?!baz).)+)$
Where:
^ # Anchor to beginning of string
( # Capture
(?: # Complex expression:
(?!foo) # make sure we're not at the beginning of string
literal "foo"
(?!bar) # and we're not at the beginning of string literal
"bar"
(?!baz) # and we're not at the beginning of string literal
"baz"
. # accept any character
)+ # at least once (to ensure string is not empty)
) # End capture
$ # anchor to end of string
-- Tim Uttormark
Principal Java Architect, Peopleclick, Inc.
________________________________
From: [email protected] [mailto:[email protected]] On
Behalf Of Tim Jowers
Sent: Monday, April 21, 2008 10:01 AM
To: Java Users Group
Subject: [Juglist] regex for any word from a set of words
Hi,
I'm seeking to define a regular expression to match on one of a set of
words. Does anyone have a trick for this? Does anyone have a trick for
negative search (find all but strings having a word)?
Specifically, EMC Smarts network monitoring software allows subscription
to events based on a regular expression. We do not want noise events
like "Unresponsive" or "Duplicate".
To match all, I can use:
.*
To match the ones I do NOT want I could use:
.*Unresponsive$ to find matches to with Unresponsive
.*[U|D][n|u][r|p][e|l][s|i][p|c][o|a][n|t]s?i?v?e"
But of course the last one also matches other letter combinations like
"Unplicate". :-) This could be OK for our scenario as we have defined a
small set of event types.
But what about NOT having a word. I've searched for this on the net for
about 1.5 hours and tried various things but does anyone know a good way
to write a regular expression to match any strings *without* a word from
a set of words?
I can contrive something to match anything except the word
"Unresponsive" for instance:
".+?_(?!U(?:n(?!r(?:e(?:s(?!p(?:o(?!s)))))))).+?"
Unfortunately, the "groups" created with parenthesis really are not much
use in Pattern.matches but only when iterating for matches. E.g. I can
find matches with:
String regex = ".+?com_(?!Unresponsive)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(candidateString);
while (matcher.find()) {
System.out.println("MATCH:" + matcher.group() );
}
The (?! signifies the negative search and works fine for making groups
but does not seem to work for testing if an expression matches or not. I
can try the ^ operator but like everything else it only seems to apply
to the next character.
My reasoning is the regular expression logic creates a match graph based
on characters so any "word" matching really has to be distilled to
character matching which make the use of a Regular Expression for this
task a tedious challenge at best and error prone at least. Has anyone
found a class or library with a Regular Expression style functionality
but operating on Strings? Or Strings as well as characters?
Thanks!
TimJowers
_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org