Results 1 to 9 of 9
Thread: regular expression issue
- 07-22-2012, 08:38 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
regular expression issue
Hi,
I use the method String.matches(String regex) to find if a string matches the regex expression
From my point of view the regular expression regex="[0-9]+" means a String that contains at least one figure between 0 and 9
But when I debug "3.5".matches("[0-9]+") it returns false.
So what is wrong ?
- 07-22-2012, 08:51 AM #2
Re: regular expression issue
"[0-9]+" means a string that contains at least one figure between 0 and 9, and nothing else.
If you wanted to match "3.5", you could do it a variety of ways. But if you're trying to match valid decimal numbers, you'll need to consider a few things. Do you want to allow leading and trailing zeros? Must there be a numeral before the decimal point? Et cetera.Get in the habit of using standard Java naming conventions!
- 07-22-2012, 08:55 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: regular expression issue
The matches( ... ) method tries to match the entire String against the pattern. In your case it fails for obvious reasons. Use a Pattern object and a Matcher object instead and use the method(s) in the Matcher class to find matches anywhere in the String.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-22-2012, 09:01 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: regular expression issue
Like any API the Java classes attempt to "do the right thing". But when the right thing fails to happen we have to check the API docs.From my point of view the regular expression regex="[0-9]+" means a String that contains at least one figure between 0 and 9
String matches() says "An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str)". So we look at Pattern's matches() which sends us on our way to Matcher's matches(). (Basically the regex is compiled to a pattern from which a Matcher instance is created based on the string we are matching against). Finally! And we find "Attempts to match the entire region against the pattern".
The problem is that the String method matches the entire string against the pattern.So what is wrong ?
-----
Reading those API docs is one way of approaching the likely next question. Or read about regular expressions in Oracle's Tutorial.
- 07-22-2012, 10:00 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: regular expression issue
Also at javaprogrammingforums.com
Please, if you are going to start a discussion in multiple forums, post links letting everyone know where else the discussion might be taking place. Bear in mind, too, that many people won't spend time on questions that might well have been answered elsewhere.
- 07-24-2012, 05:17 AM #6
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: regular expression issue
pbrockway2, how can you tell when a question is cross-posted? Do you have a program that searches the two forums for duplicate titles?
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-24-2012, 05:58 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: regular expression issue
Like a number of people here I'm something of an addict - although not to the extent of some, who couldn't stop no matter what they might claim! A while back this forum's database went sour for a while and I ended up at jpf.com for a fix. I just stayed. The way you do.
I draw attention to multiple posts as a service to others. (and not with any heat, or antagonism) Also to push my (minority!) view of forums as places of collegial discussion not just somewhere to obtain teh codez. Think a stoa in Athens, not the temple at Delphi.
Of course there is software that quickly detects the antics of these fools, and some posts cry out for it. Consider Google, multiposters! And repent!
- 07-24-2012, 08:08 AM #8
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: regular expression issue
Wow, that's pretty cool!
Also, I'm not trying to make a joke here, I swear I'm just genuinely curious: Did somebody already register as pbrockway before you?"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-24-2012, 08:56 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Regular expression
By garnaout in forum New To JavaReplies: 4Last Post: 05-15-2012, 06:22 PM -
Regular Expression issue and setName() method issue
By geforce in forum New To JavaReplies: 2Last Post: 01-30-2012, 03:33 AM -
regular expression
By ehsansad in forum New To JavaReplies: 8Last Post: 03-18-2011, 02:23 PM -
regular expression
By QkrspCmptPop in forum Advanced JavaReplies: 8Last Post: 01-20-2010, 03:55 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks