2016-09-21 2 views
2

URI에서 UUID를 추출해야하고 지금까지 50 %의 성공을 거둘 수 있어야합니다. 제게 나에게 정확히 일치하는 정규식을 제안 할 수 있습니까?자바 정규 표현식을 사용하여 URI에서 UUID를 추출하는 방법

public static final String SWAGGER_BASE_UUID_REGEX = ".*?(\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12})(.*)?"; 

public static final String abc="https://127.0.0.1:9443/api/am/store/v0.10/apis/058d2896-9a67-454c-95fc-8bec697d08c9/documents/058d2896-9a67-454c-9aac-8bec697d08c9"; 
public static void main(String[] args) { 
    Pattern pairRegex = Pattern.compile(SWAGGER_BASE_UUID_REGEX); 
    Matcher matcher = pairRegex.matcher(abc); 

    if (matcher.matches()) { 
     String a = matcher.group(1); 
     String b = matcher.group(2); 
     System.out.println(a+ " ===========> A"); 
     System.out.println(b+ " ===========> B"); 
    } 
} 

내가 현재 받고 있어요 출력은

058d2896-9a67-454c-95fc-8bec697d08c9 ===========> A 
/documents/058d2896-9a67-454c-9aac-8bec697d08c9 ===========> B 

지금 내가 B의 출력은 단지

058d2896-9a67-454c-9aac-8bec697d08c9 

어떤 도움은 매우 극명하게 될 것이다되고 싶은 것입니다! 감사합니다

답변

4

matches()을 사용하면 전체 문자열을 일치시키고 2 개의 캡처 그룹을 정의 할 수 있습니다. 일치 항목을 찾으면 그룹 1 (즉, 처음 발견 된 UUID)을 인쇄 한 다음 그룹 2의 내용 ()을 첫 번째 UUID ((.*)으로 캡처 한) 다음에 오는 나머지 문자열을 인쇄합니다.

전체 문자열을 일치시키지 않고도 UUID 패턴이 여러 번 일치하는 것이 좋습니다. 간단한 "\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}" 정규식 Matcher.find을 사용

public static final String abc="https://127.0.0.1:9443/api/am/store/v0.10/apis/058d2896-9a67-454c-95fc-8bec697d08c9/documents/058d2896-9a67-454c-9aac-8bec697d08c9"; 
public static final String SWAGGER_BASE_UUID_REGEX = "\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}"; 

public static void main (String[] args) throws java.lang.Exception 
{ 
    Pattern pairRegex = Pattern.compile(SWAGGER_BASE_UUID_REGEX); 
    Matcher matcher = pairRegex.matcher(abc); 
    while (matcher.find()) { 
     String a = matcher.group(0); 
     System.out.println(a); 
    } 
} 

Java demo058d2896-9a67-454c-95fc-8bec697d08c9058d2896-9a67-454c-9aac-8bec697d08c9 출력을 참조하십시오.

+1

그것은 좋은 설명을 위해 일했고 감사합니다. – Infamous