Re: Regex in java



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 );
}
}
}
}


.



Relevant Pages

  • Re: Regex in java
    ... string data that is actually a BCD number. ... I'm not a regex expert, but I'll give it a go. ...
    (comp.lang.java.programmer)
  • Re: Extracting numbers from a single sell
    ... try using the following Regex instead: ... Start with a digit ... The next three or more characters can be a hyphen, digit, space or ... So it should pick up all five digit string, even if they include the extra ...
    (microsoft.public.excel.programming)
  • Re: Regex in java
    ... String value= ... Regex that i have used is some thing like this ... Am not sure if this is really BCD? ... This is much easier than fiddling with regexp. ...
    (comp.lang.java.programmer)
  • Re: best design for parse- resent please ignore prev
    ... That is the rationale I let user somehow pick the date format mask. ... worse when 2 digit year is used. ... Private Sub ProcessData(ByVal source As String) ... I can transform the date format mask to regex in the ...
    (microsoft.public.dotnet.languages.vb)
  • Re: best design for parse- resent please ignore prev
    ... That is the rationale I let user somehow pick the date format mask. ... worse when 2 digit year is used. ... Private Sub ProcessData(ByVal source As String) ... I can transform the date format mask to regex in the ...
    (microsoft.public.dotnet.languages.vb)