Search code examples
javaregexstringmatchingcapturing-group

RegEx: Grabbing value between quotation marks from string


This is related to: RegEx: Grabbing values between quotation marks.

If there is a String like this:

HYPERLINK "hyperlink_funda.docx" \l "Sales"

The regex given on the link

(["'])(?:(?=(\\?))\2.)*?\1

is giving me

[" HYPERLINK ", " \l ", " "]

What regex will return values enclosed in quotation mark (specifically between the \" marks) ?

["hyperlink_funda.docx", "Sales"]

Using Java, String.split(String regex) way.


Solution

  • You're not supposed to use that with .split() method. Instead use a Pattern with capturing groups:

    {
        Pattern pattern = Pattern.compile("([\"'])((?:(?=(\\\\?))\\3.)*?)\\1");
        Matcher matcher = pattern.matcher(" HYPERLINK \"hyperlink_funda.docx\" \\l \"Sales\" ");
    
        while (matcher.find())
            System.out.println(matcher.group(2));
    }
    

    Output:

    hyperlink_funda.docx
    Sales

    Here is a regex demo, and here is an online code demo.