5

I want to write a regular expression in java which will accept the String having alphabets, numbers, - and space any number of times any where.

The string should only contain above mentioned and no other special characters. How to code the regular expression in java?

I tried the following, It works when I run it as a java application.

But the same code when I run in web application and accept the values through XML, It accepts '/'.

 String test1 = null;

 Scanner scan = new Scanner(System.in);
 test1 = scan.nextLine();

 String alphaExp = "^[a-zA-Z0-9-]*$";

 Pattern r = Pattern.compile(alphaExp);
 Matcher m = r.matcher(test1);

 boolean flag = m.lookingAt();

 System.out.println(flag);

Can anyone help me on this please?

Nidhee
  • 75
  • 7
  • 4
    [Doesn't match for me](http://ideone.com/GwV5eM). – Andy Turner May 23 '16 at 13:11
  • Is this the correct solution according to you? – Nidhee May 23 '16 at 13:13
  • 3
    Yes. But more importantly, is that the correct solution according to *you*? – Andy Turner May 23 '16 at 13:13
  • 1
    In other words: [You want to exclude all special characters](http://stackoverflow.com/questions/756567/regular-expression-for-excluding-special-characters). – KJaeg May 23 '16 at 13:13
  • @Nidhee your example does not replicate. A single `/` is not matched. – Mena May 23 '16 at 13:14
  • Yes @KJaeg but not '-'. – Nidhee May 23 '16 at 13:15
  • @Nidhee, Oh, didn't see the "-" in your text because it was located at an unlucky position in your text. Does it help to put it in a character class, like in the following Link? http://stackoverflow.com/questions/3796179/how-to-include-a-minus-sign-in-this-regex You could also try to escape it with a backslash: "\-" – KJaeg May 23 '16 at 13:20

3 Answers3

0

You can try to use POSIX character classes (see here):

Pattern p = Pattern.compile("^[\\p{Alnum}\\p{Space}-]*$");
Matcher m = p.matcher("asfsdf 1212sdfsd-gf121sdg5 4s");
boolean b = m.lookingAt();

With this regular expression if the string you pass contain anything else than alphanumeric or space characters it will be a no match result.

perbellinio
  • 682
  • 5
  • 14
0

I think you're just missing a space from the character class - since you mentioned it in your text ^[a-zA-Z0-9 -]*$

You can add the Pattern.MULTILINE flag too so you can specify how the pattern handles the lines:

     String alphaExp = "^[a-zA-Z0-9 -]*$";

     Pattern r = Pattern.compile(alphaExp, Pattern.MULTILINE);
     Matcher m = r.matcher(test1);

     boolean flag = m.lookingAt();
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
0

Pay attention to the fact that * quantifier will make it match to everything including no matches (0 or more times, like empty lines or blank tokens "", infinitely.

If you instead use + "[\w\d\s-\]+" it will match one or more (consider using \\ for each \ in your Java Regex code as follow: "[\\w\\d\\s-]+"

Consider that * is a quantity operator that works as {0, } and + works like {1, }

Jaumzera
  • 2,305
  • 1
  • 30
  • 44
  • Since its enclosed between `^` and `$`, the `*` quantifier makes perfect sense. – Tamas Rev May 24 '16 at 08:01
  • Yes, it does, but his code is taking input from a scanner.nextLine, so each input string will be a single line already. And using * it will match also blank strings while + doesn't. – Jaumzera May 24 '16 at 12:53