regex for any word from a set of words
"Tim Jowers" <[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <[email protected]> |
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