4

I am new to the concept regular expression in Java. how to define a regular expression which matches the phrase "money", "more money" but not the phrase "no money" ?

demo_user
  • 461
  • 2
  • 7
  • 17
  • Are there any other possible phrases?? – prasanth Jun 14 '13 at 09:08
  • 1
    Use a negative look-behind to prevent a preceeding "no". – aioobe Jun 14 '13 at 09:08
  • 1
    If you only want to match `money` and `more money` and nothing else, `string.matches("money|more money")` would work. If there are some additional constraints, you'll have to list them. – Bernhard Barker Jun 14 '13 at 09:09
  • I should write a method with a string as argument. The output has to be the result whether the string matches or not. – demo_user Jun 14 '13 at 09:10
  • if you want to match just these two phrases why not use if else? – voidMainReturn Jun 14 '13 at 09:10
  • 1
    What is the exact condition of matching? If you don't want to match a specific substring, see this question: http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word – thSoft Jun 14 '13 at 09:11

3 Answers3

3

It would be something like that

(more )?money

If you would need more words included:

(more |less )?money

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
1

To match all strings that contain "money" without "no" in front use negative lookbehind:

(?<!no )money

Here are some examples. Note that I added .* before and after the regular expression to match anywhere in the string:

re = ".*(?<!no )money.*"
"blah blah blah money blah blah blah".matches(re)
=> true
"blah blah blah more money blah blah blah".matches(re)
=> true
"blah blah blah no money blah blah blah".matches(re)
=> false
tom
  • 21,844
  • 6
  • 43
  • 36
1

You could do this with regexes ... and I think "(?<!no)( more)? money" should do the trick for your example.

But this is not a good way to analyse text:

  • It can give false positives; e.g.

    "domino money"  -- matches
    

    and false negatives; e.g.

    "more    money" -- doesn't match because we didn't allow for multiple spaces.
    
  • As the language you are testing get richer, you end up with combinatorial explosion in your regexes. This makes your regexes hard to read / maintain, and can lead to performance problems.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216