Re: Regex in java
- From: Mark Space <markspace@xxxxxxxxxxxxxx>
- Date: Fri, 18 Apr 2008 07:51:20 GMT
programmer.sajjad@xxxxxxxxx wrote:
Hi computer scientists/developers/engineers....
Can some one solve my problem, actually i want to check validity for
string data that is actually a BCD number.
Valid data is in following format:
String value=
"5.5" or "-5.5" or "54.598" or "-.5" or "5" or "128" etc
Regex that i have used is some thing like this
void checkValid(String value){
if((value.trim().matches("-??[0-9]{1,}[^A-Za-z].??[0-9]{1,}")) ||
(value.trim().matches("[^A-Za-z]-??.??[0-9]{1,}")) ){
this.value=value.trim();
}
else{
throw new IllegalArgumentException("Invalid BCD character("+value
+") ");
}
but when i give only '5' its not working
I'm not a regex expert (say that three times fast), but I'll give it a go.
First, is trim() really a good idea? Seems to me regex can skip over white space.
I don't really understand why you are using quantifiers. Those "??" everywhere don't seem to be needed. I not saying it's wrong, just pointing out I didn't parse that part very well.
Also, shouldn't the . in ??.?? be escaped? I assume that's a literal decimal point there, not a regex reserved word.
Ok, my first attempt is:
"\\s*-?[0-9]*\\.?[0-9]*\\s*"
That's the Java string. The regex would be "\s*-?[0-9]*\.?[0-9]*\s" after Java is done with the \ escaping. This seems to work ok. It doesn't require a trim() either.
But the above regex does allow just one "-" or "." to match. So I think it needs either one digit before the . or one after to be ok.
Second attempt:
"\\s*-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)\\s*"
This is better. It does match "5.", and it is a little verbose. Maybe you don't want to match patterns like "5.". Some thinking on my part yeilds:
"\\s*-?[0-9]*\\.?[0-9]+\\s*"
So I think that's it. (Now I'm not sure what you were trying to do with {1,}.) Clearly, having no leading digits like "-.5, or trailing digits like just "5", is ok. My regex uses "one or more" ("+") on the second part, relying on the greedy "*" to accept digits until a "." or a white space is found. If a "." is found, it must be followed by at least one digit. The "+" also says that at least one digit must be present -- white space alone won't match.
Here's my test harness:
public class Main {
static String [] tests = {
"5",
"-.5",
"5.5",
" ",
" 5 ",
" -5.5",
" - 3.1419",
"5.",
"5.5.5.5",
"-.",
".",
"-",
""
};
static String regex ="\\s*-?[0-9]*\\.?[0-9]*\\s*";
public static void main(String[] args) {
for( String s : tests )
{
if( s.matches( regex ) )
{
System.out.println( s + " matches." );
}
else
{
System.out.println( "No match on "+s );
}
}
}
}
.
- Follow-Ups:
- Re: Regex in java
- From: programmer.sajjad@xxxxxxxxx
- Re: Regex in java
- References:
- Regex in java
- From: programmer.sajjad@xxxxxxxxx
- Regex in java
- Prev by Date: Re: Test driven design and graphics in Java
- Next by Date: Re: Regex in java
- Previous by thread: Re: Regex in java
- Next by thread: Re: Regex in java
- Index(es):
Relevant Pages
|