Regular Expression - to check ( NOT 100)
I need a simple regular expression.
I just need to check the number is not 100. i.e the regular expression should return false only if the string is not number or number is 100.
e.g. :
abc - false
156abc - false
1 - true
10 - true
68 - true
100 - false
789 - true
1000 - true
[343 byte] By [
sivakilaa] at [2007-11-27 6:50:30]

I tried the following patterns... it didn't work
^[^(100)]$
^[^(100)]\\d+$
import java.util.regex.* ;
public class Test
{
public static void main(String[] arg)
{
// Pattern p = Pattern.compile("^(100)\\d+$");
Pattern p = Pattern.compile("^\\d*[^(100)]$");
Matcher m1 = p.matcher("100");
System.out.println(m1.matches());
Matcher m2 = p.matcher("abc");
System.out.println(m2.matches());
Matcher m3 = p.matcher("1001");
System.out.println(m3.matches());
Matcher m4 = p.matcher("10");
System.out.println(m4.matches());
Matcher m5 = p.matcher("888");
System.out.println(m5.matches());
}
}
in the above example i expect only the first and second SOP print false
Does it have to be done by regular expression? Would seem simpler to first test for a number, and then check the value.
Yes, it should be done only by regular expression. NOTE: My real problem is in XSD for one tag i can allow only numeric except the number 100. In XSD there is no way specify this. Only way is to use the regular expression.
1. Why regular expression?2. If RE then why don't check IS 100 then reverse result?3. Ok, may be you have reasons like homework..."^(1|10|[^1].*|1[^0].*|10[^0].*|100.+)$"
Doing this with a regular expression is a pretty weird idea, but this should do the job:((?!100\\b)\\b\\d+\\b)
> this should do the job:false for "abc" string ;)
> 1. Why regular expression?
> 2. If RE then why don't check IS 100 then reverse
> result?
> 3. Ok, may be you have reasons like homework...
>
> "^(1|10|[^1].*|1[^0].*|10[^0].*|100.+)$"
And then replace the . (dots) with \\d, otherwise every String with a non-digit character in it would become a valid one.
> > this should do the job:> > false for "abc" string ;)Which is the desired output.; )
Arghh!!!It was my great inattention... I thought only 100 should provide false... Ok then ;)
String[] values =
{
"abc",
"156abc",
"1",
"10",
"68",
"100",
"789",
"1000",
};
for (String value : values)
{
System.out.println(value + "\t" + value.matches("(?!100)\\d+|\\d{4,}"));
}
Sabre, this regular expression will work in the case of String.matches because it matches against the whole string, although it won't work when trying to find many matches inside the same String ;)Of course the OP didn't say anything about it, just wanted to point it out.
Or even easier System.out.println(value + "\t" + value.matches("(?!100$)\\d+"));
> Sabre, this regular expression will work in the case
> of String.matches because it matches against the
> whole string, although it won't work when trying to
> find many matches inside the same String ;)
>
> Of course the OP didn't say anything about it, just
> wanted to point it out.
It does not make sense to test for not 100 within the string since 1000 is allowed! My second solution does what was asked so unless the OP has a creeping specification then I will stick with it.
> It does not make sense to test for not 100 within the
> string since 1000 is allowed! My second solution does
> what was asked so unless the OP has a creeping
> specification then I will stick with it.
Yet with the regex I posted earlier it is possible to test for not 100 within the String and still allow for 1000 :)