How to extract a substring using regex

前端 未结 14 1447
暖寄归人
暖寄归人 2020-11-22 13:37

I have a string that has two single quotes in it, the \' character. In between the single quotes is the data I want.

How can I write a regex to extract

14条回答
  •  無奈伤痛
    2020-11-22 14:23

    Assuming you want the part between single quotes, use this regular expression with a Matcher:

    "'(.*?)'"
    

    Example:

    String mydata = "some string with 'the data i want' inside";
    Pattern pattern = Pattern.compile("'(.*?)'");
    Matcher matcher = pattern.matcher(mydata);
    if (matcher.find())
    {
        System.out.println(matcher.group(1));
    }
    

    Result:

    the data i want
    

提交回复
热议问题