Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regex class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regex class" Watch "regex class" New topic
Author

regex class

Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

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

Joined: Apr 06, 2010
Posts: 4246
    
    7

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

Joined: Jul 24, 2012
Posts: 41

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

Joined: Apr 06, 2010
Posts: 4246
    
    7

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

Joined: Jan 05, 2012
Posts: 82

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

Joined: Jul 24, 2012
Posts: 41

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

Joined: Apr 04, 2012
Posts: 923
    
    1

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


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex class
 
Similar Threads
Telephone No Checking
New syntax in Java 6?
Regex doubt
Regular expression
Java number parsing in French locale