• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex class

 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pattern p = Pattern.compile("\\d\\d\\d([-\\s])?\\d\\d\\d\\d");

want to search for a seven digit number (phone number)
1234567
123 4567
123-4567
are all correct findings...
this finder fails at the 123 4567 one
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah you sure that's exactly what you're testing? I ask because I just tried it out using http://www.regexplanet.com/advanced/java/index.html, and all three matched.
 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Ah you sure that's exactly what you're testing? I ask because I just tried it out using http://www.regexplanet.com/advanced/java/index.html, and all three matched.

ya they work on your given link but on my command line one of them doesnt .... Strange!
thankyou for the help..
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you entering things via the command line then? What? And how are you doing it? Try printing out the regular expression and the test strings, to make sure they're actually what you think they are.
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya they work on your given link but on my command line one of them doesnt .... Strange!


IF you are passing from command line, you have to use double quotes like "123 4567" to include spaces ,,,,
 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don Redd wrote:
ya they work on your given link but on my command line one of them doesnt .... Strange!


IF you are passing from command line, you have to use double quotes like "123 4567" to include spaces ,,,,


IT WORKED NOW!
thanks man thankyou so much!
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
commanline treats space character as a seperator. if you need to pass commandline arguments containing spaces or other special characters as don said, you need to enclose it in double quotes to escape them so that the space character is treated as LITERAL SPACE in commandline lingo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic