File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes regex prblem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regex prblem" Watch "regex prblem" New topic
Author

regex prblem

akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

i have string


i have to get the 14 from this

i use


but getting 11 how i can get the 14
it should be generic e.g the value of 14 can be changed


Start Earning Online||Start Earning Using Java
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40


One option is to call the find() method again, to get the next match. Another is to use look aheads / look behinds to place a restriction of stuff around the match -- such as only return the match if it comes after the word "of".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try calling find() again.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

When I change my signature this response will no longer make sense.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'm not quite sure what you are trying to match with the regex and why 14 is the correct answer but 11 is not.
is it "page ## of ##" plus additional white-space and you want to match the second number?
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

David O'Meara wrote:I'm not quite sure what you are trying to match with the regex and why 14 is the correct answer but 11 is not.
is it "page ## of ##" plus additional white-space and you want to match the second number?


it is " page ## of ## " and i need the ## that occur in the las ## can also be # if ## is less then 10
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

it also depends whether you will allow trailing whitespace, like you have in your example.
Regular expressions are greedy by default and will match as much as possible so the expression "match numbers at the end of the line" will work, but may need to be "match numbers at the end of the line, but it may have whitespace between the number and the end of the line"

[0-9]+ or \\d+ both match one or more numbers but will grab all of the numbers that they can
\\s* will match zero or more whitespace characters
$ signifies the end of line
BUT you only want the number part, so we group it using round brackets
(\\d+)\\s*$
Keep in mind you want to remove group #1 from the matched section which isn't much harder than find()
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

thanks David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex prblem