2014-05-15 3 views
-2

다음 규칙을 사용하여 정규식을 작성하려고합니다.따옴표를 검사하면 정규식이 일치합니다.

정규식 일치 할 필요가있는 텍스트 :

Key: "employeeNo" with value "ABC12345" is already used. 

규칙 :

1. Only the number after ABC is changing. 

2. Key: "employeeNo" with value "ABC[Any Number with any length]" is 
    already used. 

진정한 경우

Key: "employeeNo" with value "ABC12345" is already used. 
Key: "employeeNo" with value "ABC987858547" is already used. 
Key: "employeeNo" with value "ABC7" is already used. 

Falsecases

key is not used 
Key: "employeeNo" with value "ABCXYZYZ" is already used. 
Key: employeeNo with value ABC7 is already used. 

정규식은 "나는 또한"따옴표를 확인 할 Key: "employeeNo" with value "ABC[0-9]+" is already used.

을 시도했다.

+4

질문을 이해할 수 없습니다. 명확히하십시오. 어떤 언어/도구를 사용하고 있으며 '나는 또한 따옴표를 확인하고 싶습니까?'란 무엇을 의미합니까? – HamZa

+0

@ HamZa 혼란에 대해 죄송합니다. 질문을 업데이트했습니다. 자바를 사용하고 있습니다. 나는 따옴표 ""를 확인하고 싶다. 너 나 좀 도와 줄 수있어? – Patan

+1

무엇을 의미합니까? 문제가 어디 있니? 하나 또는 두 개의 입력에 예상 출력을 제공 할 수 있습니까? – HamZa

답변

0

간단하게 보면 큰 따옴표를 이스케이프 처리해야합니다. 당신이 백 참조 그룹 (그룹 1) 내부의 큰 따옴표를해야하는 경우

String input = "Key: \"employeeNo\" with value \"ABC12345\" is already used."; 
Pattern p = Pattern.compile("Key: \"employeeNo\" with value \"(ABC\\d+)\" is already used."); 
Matcher m = p.matcher(input); 
if (m.find()) { 
    System.out.println(m.group()); 
    System.out.println(m.group(1)); 
} 

출력

Key: "employeeNo" with value "ABC12345" is already used. 
ABC12345 

, 그냥 그룹 괄호 안에 탈출 따옴표로 묶어야 : (\"ABC\\d+\").

관련 문제