• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to extract a group of digits in a String?

 
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Given the String "xyz_Q1229_F1224", how can I extract the 1229 from it?

I tried:
https://ideone.com/SiCrRs



But this is returning '9'. Why is it returning the last digit of the first digit-group 1229?

edit:
Argh, found it -> Q(\\d+)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done and thank you for telling us the solution.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just use the substring method? It's much simpler and easier to understand.

Or are there some restrictions that you haven't clearly explained? (hint hint)

My larger point is that you should get in the habit of clearly explaining what you need to do. An example is always great, but it doesn't define the problem. Given your description, there is no reason that substring(5,9) wouldn't work (assuming I have my indexes correct).

If the number of digits can vary, you should include that in your problem description.

Just my 2cents
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Answering the original question, even though it is no longer needed...

Sam Samson wrote:
But this is returning '9'. Why is it returning the last digit of the first digit-group 1229?



When a regular expression repeats a capturing group (for completion of the match), only the last iteration is returned for the capturing group.

Henry
 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Why not just use the substring method? It's much simpler and easier to understand.

Or are there some restrictions that you haven't clearly explained? (hint hint)

My larger point is that you should get in the habit of clearly explaining what you need to do. An example is always great, but it doesn't define the problem. Given your description, there is no reason that substring(5,9) wouldn't work (assuming I have my indexes correct).

If the number of digits can vary, you should include that in your problem description.

Just my 2cents



Jep, sorry, the number of digits can vary.
 
reply
    Bookmark Topic Watch Topic
  • New Topic