By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Need some regex help here please :)

P: 7
I suck at regex's and I need a little hope. Hopefully there is a guru out there to help me on this one. I want to do a match on the following:

one or more numeric characters [0-9]
zero or more blank spaces \s
zero or more dash characters -

I've tried everything I know for now, but I still can't get my unit test to pass my conditions above. Anyone out there who could help? Thanks so much.
Nov 29 '06 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I suck at regex's and I need a little hope. Hopefully there is a guru out there to help me on this one. I want to do a match on the following:

one or more numeric characters [0-9]
zero or more blank spaces \s
zero or more dash characters -

I've tried everything I know for now, but I still can't get my unit test to pass my conditions above. Anyone out there who could help? Thanks so much.
What does your program look like right now?
Nov 29 '06 #2

P: 7
What does your program look like right now?
Well, I've tried several things. Here are a few:

mystring.matches("(\\d*)?(-*)?(\\s*)?")

and I've tried things this way:

Pattern patter1 = Pattern.compile("(\\d)+(\\s)*(-)*");
Matcher m = patter1.matcher(mystring);
return m.find();
Nov 29 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Well, I've tried several things. Here are a few:

mystring.matches("(\\d*)?(-*)?(\\s*)?")

and I've tried things this way:

Pattern patter1 = Pattern.compile("(\\d)+(\\s)*(-)*");
Matcher m = patter1.matcher(mystring);
return m.find();
Are you specifically grouping them to pull them out? If not, you can omit the parentheses - have you tried [0-9]+ for the digits?

Is there anything on the line either before or after that is matched? You might need a .* to continue the search through the end of the line...

What are you trying to parse/find?
Nov 29 '06 #4

P: 7
Are you specifically grouping them to pull them out? If not, you can omit the parentheses - have you tried [0-9]+ for the digits?

Is there anything on the line either before or after that is matched? You might need a .* to continue the search through the end of the line...

What are you trying to parse/find?
I need to verify a serial number is correct. Ok this is what I have now:

public static boolean validateValue(String value)
{
Pattern p = Pattern.compile("[0-9]+[-]*[\\s]*");
Matcher m = p.matcher(value);
return m.find();
}

The thing is the string must contain as least 1 digit, but the "-" and " " characters are optional (there can be zero or more of them). No other characters are allowed. The "No other characters allowed" part is the main issue I've been having whenever I think I've solved the problem. Should I do another check of some sort before? I'm not sure how to do the nots for the optional "-" and " " characters when at least 1 digit is required.
Nov 29 '06 #5

sicarie
Expert Mod 2.5K+
P: 4,677
I need to verify a serial number is correct. Ok this is what I have now:

public static boolean validateValue(String value)
{
Pattern p = Pattern.compile("[0-9]+[-]*[\\s]*");
Matcher m = p.matcher(value);
return m.find();
}

The thing is the string must contain as least 1 digit, but the "-" and " " characters are optional (there can be zero or more of them). No other characters are allowed. The "No other characters allowed" part is the main issue I've been having whenever I think I've solved the problem. Should I do another check of some sort before? I'm not sure how to do the nots for the optional "-" and " " characters when at least 1 digit is required.
Here's Java's regex tutorial.

http://java.sun.com/docs/books/tutor...gex/index.html

Have you compiled that? It looks correct (without me actually creating tests for it and running it...). With regex's + means "1 or more" and * means "0 or more".
At the moment, that regex will pick up

1
1-
12
12-
1234567897654
23456789765-

Which is the way you are describing the serials (which seems a little odd to me). Are the integers allowed anywhere in between the '-' or the whitespace?

I think you might want something like [0-9]+[-]*[0-9]*[\\s]*[0-9]* but I'm not sure - are there a few example serials?
Nov 29 '06 #6

P: 7
Thanks so much for the help. Yes I compiled everything and it gives me correct output for any combination of white space, digits, and - characters (assuming the condition that there is at least 1 digit). The problem I am having is if someone enters (for example):

1111-11 111-x111
1x
1-x
--- x
etc.

(I will use the character 'x' here for example only, assume x is any non-digit, non-whitespace, non-dash character):
If 'x' is anywhere in the string it should fail.

And I don't write the requirements, I just code them :-)
Nov 29 '06 #7

P: 7
This gives me what I need from my test and the conditions I posed above (although it would be nice to know how to do this in one statement):

public static boolean validateValue1(String value)
{
Pattern p2 = Pattern.compile("[^0-9-\\s]");
Matcher m2 = p2.matcher(value);
if(m2.find())
{
return false;
}
Pattern p = Pattern.compile("[0-9]+[-]*[\\s]*");
Matcher m = p.matcher(value);
return m.find();
}

Thanks for all of your help!
Nov 29 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.