vicky9988 vicky9988 - 6 months ago 8
Java Question

Regex Expression for Specific string in Java

I am new to Regex expression so not able to validate the String which can have number 0-9 FGHC,and fghc *#.

I am trying with

[0-9FGHCFGHC*#]
its working with regex tool but in java its not working.I am using java 1.7

E.g.for this pattern I required it like
2314F*Ghc
12fgH#
etc.

public static void main(String[] args){
String money = "23FGhc*#";
Pattern p = Pattern.compile("[0-9FGHCfghc*#]",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(money);
if (m.matches())
System.out.println("valid:-"+ m);
else
System.out.println("unvalid:- "+m);
}


Thank in Advance for your help and it will be more help that you explain the soultion so I can have more knowledge in RegEx

AKS AKS
Answer
(?i)[0-9fghc*#]+

You need to add a quantifier at the end as well. In this case + to match these characters one or more times.

Use can also add (?i) to make it case insensitive.

Comments