Re: regex for any word from a set of words

"Uttormark, Tim" <[email protected]>
Newsgroups gmane.org.user-groups.trijug.juglist
Message-ID <9FF69050257FC0479018211CD530F8BC0C43E5C1@CORP2K3MAIL.corp.peopleclick.com>
The original post was also looking the "positive" form of this search --
a regular expression which matches the entire content of a string which
contains at least one of a set of string literals.

That would look like this:

	^(.*(?:foo|bar|baz).*)$

Where:
^			# Anchor to beginning of string
 (			# Capture
  .*			# Any characters
  (?:foo|bar|baz)	# Followed by complex expression matching string
literal "foo" or "bar" or "baz"
  .*			# Any characters
 )			# End capture
$			# Anchor to end of string


-----Original Message-----
From: Mark Merrill [mailto:Mark.Merrill-r/[email protected]] 
Sent: Monday, April 21, 2008 2:01 PM
To: Uttormark, Tim
Cc: [email protected]; [email protected]; Tim Jowers
Subject: Re: [Juglist] regex for any word from a set of words

I like that Tim.  Very elegent.

I was trying to get a negative lookup to work, making sure that a
certain
string didn't exist before (and I guess you'd have to use positive
lookup
to make sure it didn't exist after), but I was obviously making it far
too
difficult on myself.

Mark Merrill
Development Engineer


|------------>
| From:      |
|------------>
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
  |"Uttormark, Tim" <[email protected]>
|
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|------------>
| To:        |
|------------>
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
  |"Tim Jowers" <[email protected]>
|
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|------------>
| Cc:        |
|------------>
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
  |[email protected]
|
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|------------>
| Date:      |
|------------>
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
  |04/21/2008 01:52 PM
|
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|------------>
| Subject:   |
|------------>
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
  |Re: [Juglist] regex for any word from a set of words
|
 
>-----------------------------------------------------------------------
------------------------------------------------------------------------
---|





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
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.