어떻게 문자열 내 패턴과 일치 말할 수있다

Mohadeseh :

내가 그이 패턴 내 문자열의 일치라고 할 수있는 방법 : 이름과 버전이 공백없이 문자열이 될 수 있습니다 (OS의 # NAME #Version을 설치).

사무엘 실버 Moos의 :

당신은 코드를 사용할 수 있습니다으로 :

    // assuming parenthesis an sharp are pattern included:
    String s = "(install OS #testname #testversion)";
    if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
        String[] splitted = s.split("\\s");
        String name = splitted[2].replace("#", "");
        String version = splitted[3].replace(")", "").replace("#", "");
        System.out.println("name: " + name);
        System.out.println("version: " + version);
    }
    // assuming parenthesis an sharp are both not pattern included:
    s = "install OS testname2 testversion2";
    if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
        String[] splitted = s.split("\\s");
        String name = splitted[2];
        String version = splitted[3];
        System.out.println("name: " + name);
        System.out.println("version: " + version);
    }

(당신이 부품 사이에 mutliple 공백을 허용해야하는 경우, 당신은 모두 교체해야, 분할 정규식과 일치하는 정규식 \ s의 \ S +)

추천

출처http://10.200.1.11:23101/article/api/json?id=374951&siteId=1