1

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.

4
  • Thats not working. Giving - " HYPERLINK "hyperlink_funda.docx" \l "Sales" " in entirety. Commented Sep 11, 2014 at 13:20
  • I'm assuring you, it is working. Take a look here. Commented Sep 11, 2014 at 13:22
  • What language are you using? Commented Sep 11, 2014 at 13:23
  • That's working in general but I'm using Java and using String.split(regex) way. Specifically, String[] parts = " HYPERLINK \"hyperlink_funda.docx\" \\l \"Sales\" ".split("\\\\\".*?\\\\\"");. Commented Sep 11, 2014 at 13:35

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you are misunderstanding the nature of the String.split method. Its job is to find a way of splitting a string by matching the features of the separator, not by matching features of the strings you want returned.

Instead you should use a Pattern and a Matcher:

String txt = " HYPERLINK \"hyperlink_funda.docx\" \\l \"Sales\" ";

String re = "\"([^\"]*)\"";

Pattern p = Pattern.compile(re);
Matcher m = p.matcher(txt);
ArrayList<String> matches = new ArrayList<String>();
while (m.find()) {
    String match = m.group(1);
    matches.add(match);
}
System.out.println(matches);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.