• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java regular expression grouping

 
Mitch Christensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Given the following regex pattern: "(?:\\D*(\\d+)\\D*)*"

and the following test cases: "12345" "a1234" "12a34" "1234a"

I expect to see each test case result in a set of groups that maintain the original character order, and exclude any non-digit characters.

My fallback is to iterate through the string char-by-char and build a new string skipping over non-numeric characters, but I am curious why this doesn't work for "12a34". In stead of returning the three groups "12a34" "12" "34", it returns "12a34" "34".

Where did the "12" group go?

Thanks.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just trying to pull out the digits maybe you should simplify.


output:
Pattern: (\d+) string=12345
matched: 12345
Pattern: (\d+) string=a1234
matched: 1234
Pattern: (\d+) string=12a34
matched: 12
matched: 34
Pattern: (\d+) string=1234a
matched: 1234
Pattern: (\d+) string=1234512345
matched: 1234512345
Pattern: (\d+) string=a1234a5234
matched: 1234
matched: 5234
Pattern: (\d+) string=a1234a5234
matched: 1234
matched: 5234
Pattern: (\d+) string=1234a5234a
matched: 1234
matched: 5234
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use a tool called RegExCoach that can step through the groups and highlight them one at a time. You can interactively tweak your expressions and see how they respond. See it here: http://www.weitz.de/regex-coach/ This kind of experimentation is a fun way to learn.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan. I'm gonna check it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic