Regular expression for string
- For double value with % symbol
package com.javanotes2all.java.string;
public class RegulerExpresion {
public static void main(String[] args)
{
String s="10.0%";
if(s.trim().matches(".?[0-9]+(.[0-9]+)*\\%?"))
{
System.out.println("valid string");
}else
System.out.println("invalid string");
}
}
For comma separated number
package com.javanotes2all.java.string;
public class RegulerExpresion {
public static void main(String[] args)
{
String s="1234123,123213213,123123,123123,123123,123123";
if(s.trim().matches("[0-9]+(,[0-9]+)*"))
{
System.out.println("valid string");
}else
System.out.println("invalid string");
}
}
note:1.Expression [0-9]+(,[0-9]+)* is not applicable for 1212,343434,343434,343434,3434,
Some more combinations:
No postfix/prefix commas: [0-9]+(,[0-9]+)*
No prefix (optional postfix): [0-9]+(,[0-9]+)*,?
No postfix (optional prefix): ,?[0-9]+(,[0-9])*
Optional postfix and prefix: ,?[0-9]+(,[0-9]+)*,?
0 comments:
Post a Comment