-1

I have a problem with a regexp in a java validation.

RegExp: "Office Boss|Standard Employee" The string I am trying to validate with the expression is "Office Boss" for example, and when I use "matches" function it always return "false".

I am pretty sure it is for the white space, but how can I solve it? I want to validate if the text is one of the two values in the expression only.

Thanks.

Pedro
  • 115
  • 11

2 Answers2

0

You need to wrap your expression in parentheses.

Pattern.compile("(Office Boss|Standard Employee)");
George
  • 2,820
  • 4
  • 29
  • 56
-1

You need to call find() before accessing your matched groups.

Pattern pattern = Pattern.compile("Office Boss|Standard Employee");
final Matcher matcher = pattern.matcher("Office Boss");
matcher.find();
System.out.println(matcher.group(0));
SebastianK
  • 712
  • 4
  • 19
  • If OP did not use `find()` the result would be an `Exception in thread "main" java.lang.IllegalStateException: No match found`, but OP gets `false` as a result – Wiktor Stribiżew Nov 05 '19 at 14:36