aspose file tools*
The moose likes Java in General and the fly likes What is wrong with the regex? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is wrong with the regex?" Watch "What is wrong with the regex?" New topic
Author

What is wrong with the regex?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4463
    
    8

That will only match a single number. You need [0-9]+ to match any number.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Evwn this would return flase when I tried with the following string:


Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

There is a space in your test String but your regex doesn't allow that.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

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

Joined: Oct 25, 2008
Posts: 2700

Then it matches. You can test it with the regex test harness. It will return that it finds 2 groups.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

I tried the following:

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Joe Harry wrote:I tried the following:



Matcher.matches() requires the whole string to match. You need Matcher.find() . Check the Javadoc for Matcher.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

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

Joined: Sep 07, 2004
Posts: 781

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

Joined: Oct 25, 2008
Posts: 2700

And did you check out the link I provided?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

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

Joined: Sep 26, 2006
Posts: 9506
    
    2

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

Joined: Sep 07, 2004
Posts: 781

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
 
subject: What is wrong with the regex?