It should definitely work well... if you test against a matching String :)
Your problem is that "Student (male): John"
starts with an uppercase S
, and you're trying to match a lowercase s
. That's as simple as it gets!
Note that you may use [()]
to match either (
or )
:
p = p.replaceAll("[()]", "\\\\$0");
By the way, I would also point out that you could replace the lines:
p = p.replaceAll("\\(", "\\\\(");
p = p.replaceAll("\\)", "\\\\)");
Simply by using:
p = Pattern.quote(p);
Cheers!
-------------------다음은 원하는 작업을 수행하지 않습니다.
p = p.replaceAll("\\(", "\\\\(");
당신은 교체 (
로 \\\\(
. \\\\(
정규식 컴파일이다 \\(
basicly 수단이 탈출하는 \
다음 (
의 탈출하지 (
. 당신이 찾고있는 것은 다음과 같습니다.
p = p.replaceAll("\\(", "\\(");
-------------------인라인 String text = "Student (male): John";
이 정규식과 일치하지 않기 때문에student \(male\)\:\s\w+
입력 텍스트를 text = "student (male): John";
또는 첫 번째 구문 문자열로 교체phrases[0] = "Student (male)";
Ideone의 예
출처
https://stackoverflow.com/questions/22089823