Java regex content between single quotes

后端 未结 5 1392
臣服心动
臣服心动 2020-12-09 10:37

I am trying to write a regex in Java to find the content between single quotes. Can one please help me with this? I tried the following but it doesn\'t work in some cases:

5条回答
  •  有刺的猬
    2020-12-09 10:59

    Try this simple regex pattern:

    '([^\s']+)'
    

    and a test code:

    try {
        Pattern regex = Pattern.compile("'([^\\s']+)'");
        Matcher regexMatcher = regex.matcher(subjectString);
        while (regexMatcher.find()) {
            for (int i = 1; i <= regexMatcher.groupCount(); i++) {
                // matched text: regexMatcher.group(i)
                // match start: regexMatcher.start(i)
                // match end: regexMatcher.end(i)
            }
        } 
    } catch (PatternSyntaxException ex) {
        // Syntax error in the regular expression
    }
    

提交回复
热议问题