0

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

5
  • You forgot the Pattern.CASE_INSENSITIVE flag and perhaps a quantifier (+) at the end. Commented May 9, 2016 at 9:41
  • I tried with that also,but no luck:( Commented May 9, 2016 at 9:43
  • Please show the exact relevant code. There are a bunch of questions like my regex does not work in Java but works at regex101, and the reasons can be different: 1) missed flag, 2) did not execute the Matcher, 3) used matches instead of find (and vice versa). Commented May 9, 2016 at 9:44
  • Ok, you did not use + or * at the end (and at the regex tester, you had /g modifier to enable global match). Do not use regexr.com, use regex101.com. The answer below is correct. Commented May 9, 2016 at 9:49
  • yeah that is true...Thanks for you comment.it'll give me help to understand more Commented May 9, 2016 at 9:52

1 Answer 1

2
(?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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.