Java Regex/Pattern Matching

Associate
Joined
30 Dec 2008
Posts
415
Location
York
Right,

I'm tearing my hair out with this and I'm not happy. Someone from OcUK please help me!

I need to get characters in from the command line, of the form:

(NUMBER *comma* NUMBER *comma* ... ... )

When the user types this in, I need to take all of the numbers and add them to an integer array. I've used pattern matching before, but only on Strings. Is this doable? I can't seem to get it working myself. Sorry, using Java as the language.

Thanks.
 
(\d+,)*\d+

requires minimum of 1 digit, along with prefixes of other numbers with a minimum of 1 digit and a comma to separate.

Do you need to handle non-integers too? Negatives? What are all the rules.
 
Last edited:
Non integers may crop up accidentally, but they should be caught elsewhere. This only needs to find positive integers from that form and whack them in an array.

Moogle, I tried something similar to that and it never matched anything, but I probably did it wrong.

Top dog's sounds promising, but I'm a bit confused as to what it'll actually return.
 
Mine was matching single digits but topdog's was matching the commas too.

This one seems to be working good for multiple digits

"\d+(?:,\d+)*"

Minus the double quotes.
 
Yeah for iterating over the results that should suffice, the pattern I provided would just be used to validate that the input did indeed match the specification or else \d+ would happily capture the values 12 and 34 from 12+34 or something. There are probably more elegant 'all in one' ways to do both in one shot, but that would take more time for me to figure out than I'd probably save by doing it in two passes ><
 
Well, I previously tried just \\d+ and it never matched anything. There's clearly a problem elsewhere in my code, I think I might go for a shower and then try again.

Thanks for the suggestions.
 
From looking at the API as I don't really trust my memory of it, you'd need to loop over every match individually, but if you stuck it in a utility method to return a list of matches, it would be a case of just checking if there were any matches (i.e. IsEmpty() on the results)
 
http://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html

C:\Users\Topdog\Downloads>java RegexTestHarness

Enter your regex: \d+
Enter input string to search: 123,345,543+2333
I found the text "123" starting at index 0 and ending at index 3.
I found the text "345" starting at index 4 and ending at index 7.
I found the text "543" starting at index 8 and ending at index 11.
I found the text "2333" starting at index 12 and ending at index 16.

Enter your regex:
 
Sorry for leaving last night, I went to bed instead.

This morning I decided to delete and start the method again. Low and behold, \\d+decides to work fine. No idea why it didn't before, don't much care either :p

Thanks for all the help guys. Much better mood today.
 
I actually never considered it, mainly because I believed it was old hat now. I remember using it a few years ago at college, but reading then that it wasn't the best way of doing things. Maybe I'm wrong?

Edit - The Java site says it's not recommended for use any more. I guess it would have been easier to do it that way, but hey ho.
 
Last edited:
Back
Top Bottom