-2

I am checking file names with a regex.

File names can be format of

customer name - company name

I am using this regex:

private static final Pattern fileRegex = Pattern.compile("^[a-zA-Z0-9_\\-\\.\\s\\,\\[\\]()\\{\\}]+$");

But hypen sign (minus sign) is not working and it is acting like a dash. I am not sure maybe it is because of IntelliJ idea settings. how can I add minus sign to this regex?

For example this format must be valid:

test - test1 − test2

In here first one is just simple dash and the second one is minus sign.

Chris Garsonn
  • 717
  • 2
  • 18
  • 32
  • Just add a hyphen - in the regular expression without wild cards. It will accept a plain and simple hyphen – Pankaj Sati Dec 27 '18 at 09:46
  • See [this answer](https://stackoverflow.com/a/4069678/3832970), you may either add all "dashes" you want to the character class, or just use `\p{Pd}` to match all of them. – Wiktor Stribiżew Dec 27 '18 at 10:17

2 Answers2

0

Well, they are different unicode symbols after all

code points are different: 45 for dash and 8722 for minus

You have to replace \- with [\-\−]

0

Add the minus sign (\u2212) to your character set:

private static final Pattern fileRegex = Pattern.compile("^[a-zA-Z0-9_\\-\\u2212\\.\\s\\,\\[\\]()\\{\\}]+$");
mrzasa
  • 22,895
  • 11
  • 56
  • 94