473,505 Members | 14,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need some regex help here please :)

7 New Member
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
7 2343
sicarie
4,677 Recognized Expert Moderator Specialist
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
shaolinman829
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
shaolinman829
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
shaolinman829
7 New Member
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
shaolinman829
7 New Member
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

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

Similar topics

1
1320
by: Andi Reisenhofer | last post by:
Hallo folks, Just started to use regex a bit with c#. Want to do the following perhaps somebody have a tip. 1) want to remove trailing and leading whitespaces from the string. Tried this one...
6
4775
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
2
2638
by: rjb | last post by:
Hi there I have a text file which looks like the one below. I'm trying to write a program which will go through each of those lines (for each section separated by an empty line) and give me: ...
8
1384
by: Sandy | last post by:
Hello - This code was snagged by me from the Internet and altered. Its purpose is to check for swear words. It works the way it currently is, but I need it to be more generic -- i.e., I don't...
18
3014
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
2
1456
by: Luhar | last post by:
After much scouring of information on Regular Expressions from books and the web, I've come up with the this handy little Regex to parse links from HTML: ...
9
2070
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
10
3827
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
7
2041
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
3
2191
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.