Re: regex for any word from a set of words

"Shawn Hartsock" <[email protected]>
Newsgroups gmane.org.user-groups.trijug.juglist
Message-ID <[email protected]>
Flip the problem around...

listOfWords = " word, word2, word3 " // note spaces... or get fancy
with ^, $ and conditionals

myWord = "word"

String pattern = "\\W" + myWord + "\\W"

Pattern regPattern = Pattern.compile(pattern)
Matcher matcher = regPattern.matcher(listOfWords)
if(matcher.find()) {
   // do your stuff...
}

... note there are much more elegant pattern matchers but I chose a
brutish solution of forcing a non-word character to appear in front
and behind each key word. I ran this is a short groovy script before I
sent it. I think it works.

NOTE: Groovy script is great for testing out little snippets like
this... but it is important not to start to lean on Groovy-isms if you
plan on dropping the code back into straight Java.

On Mon, Apr 21, 2008 at 10:00 AM, Tim Jowers <[email protected]> wrote:
>
> 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
>
>



-- 
/** Shawn.Hartsock http://hartsock.blogspot.com/ //*/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.