Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong with the regex?

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to look out for the ouucrences of a particular sequence which is a &# followed by numbers (0 to 9) and ends with a semicolon.



I have defined the following as my regex expression, but it never matches.



I'm sure that I made a mistake. But not sure where.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will only match a single number. You need [0-9]+ to match any number.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evwn this would return flase when I tried with the following string:


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a space in your test String but your regex doesn't allow that.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The space is because I do not know how to make it look proper in my post above. If I do not leave a space and post it here, it gets shown up as its original character. You can take it as such that there is no space in my regex.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it matches. You can test it with the regex test harness. It will return that it finds 2 groups.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following:

 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:I tried the following:



Matcher.matches() requires the whole string to match. You need Matcher.find() . Check the Javadoc for Matcher.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matcher.find() prints me true. But is there a way to extract the groups? I need those sequences as an array for checking against a predefined list. I tried mat.groupCount() and it gave me 0.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Matcher.find() prints me true. But is there a way to extract the groups? I need those sequences as an array for checking against a predefined list. I tried mat.groupCount() and it gave me 0.


If you only want the decimal part then put brackets '( and ') round the [0-9]+ and use mat.group(1) .

You will keep banging your head until you spend some time learning about regular expression and Java's Pattern and Matcher classes.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did you check out the link I provided?
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. But when I tried mat.group(0), it gave me the following:



But when I loop through using the find(), it prints the matched elements.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:And did you check out the link I provided?


Oh, yes. I just did. Thanks for all the help!
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Thanks for the suggestion. But when I tried mat.group(0), it gave me the following:



But when I loop through using the find(), it prints the matched elements.


But that is what my code fragment showed you had to do!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic