15

I want to write a java Regular expression that recognises the following patterns. abc def the ghi and abc def ghi

I tried this:

abc def (the)? ghi

But, it is not recognizing the second pattern.Where am I going wrong?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
AV94
  • 1,824
  • 3
  • 23
  • 36

3 Answers3

20
abc def (the )?ghi

           ^^

Remove the extra space

vks
  • 67,027
  • 10
  • 91
  • 124
  • Are java regex different in any sense from the normal regex. Bear my ignorance.I'm asking this becaue your soln worked here. http://www.regexr.com/ . but not when I'm writing for a java regex.So,this is what i did abc def (\sthe)?ghi. this only worked when the is not there. – AV94 Sep 14 '15 at 13:45
  • @vks I wrote abc def(\\sthe)?ghi .still same result.didn't I get it right? – AV94 Sep 14 '15 at 13:52
  • @anil it should be `abc def(\\sthe)? ghi` – vks Sep 14 '15 at 13:54
  • @vks It didn't .To be specific,I'm using stanford regexner for my project.As a part of that, I wrote this regular expression.The Regex written above is working fine when the "the" part is not there.But when it is,pattern is not being recognised.Somewhere in other thread,there is a discussion that java treats spaces in a different way,I'll post the link here.Please help me if you can.Here is the link : http://stackoverflow.com/questions/4731055/whitespace-matching-regex-java – AV94 Sep 14 '15 at 16:41
  • @anil instead of `\\s` you can try ` ` literal space – vks Sep 14 '15 at 16:49
5

Spaces are also valid characters in regex, so

abc def (the)? ghi
       ^      ^ --- spaces

can match only

abc def the ghi
       ^   ^---spaces

or when we remove the word

abc def  ghi
       ^^---spaces

You need something like abc def( the)? ghi to also make one of these spaces optional.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Your example worked perfectly well for me in Python

    x = "def(the)?"
    s = "abc def the ghi"
    res = re.search(x, s)

Returns: res -> record

    s = "abc def ghi"
    res = re.search(x, s)

Returns: res -> record

    s = "abc Xef ghi"
    res = re.search(x, s)

Returns: res -> None

QuentinJS
  • 162
  • 1
  • 9