문자열의 단어와 선택적으로 일치하는 정규식은 어떻게 만들 수 있습니까? 예를 들어, 내가 문장 일치하는 정규식을 가정 해 봅시다 :정규식 - 선택적으로 단어 일치
I like vanilla ice cream.
을하지만 같은 정규식도 내가 그것을 어떻게
I like ice cream.
일치하도록 "바닐라"단어는 선택 사양 일 것을 원하는 ? 나 바닐라를 사용 했어? 그러나 그것은 작동하지 않는 것 같습니다.
String s1 = "I like vanilla icecream";
String s2 = "I like icecream";
Pattern p = Pattern.compile("I like (vanilla)?icecream");
System.out.println(p.matcher(s1).matches()); // true
System.out.println(p.matcher(s2).matches()); // true
당신이 시도하고 정확한 정규식을 게시 할 수 작동합니까? –