Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex prblem

 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling find() again.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I change my signature this response will no longer make sense.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks David O'Meara
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic