RegEx:捕获字符串中的引号之间的值 [英] RegEx: Grabbing value between quotation marks from string

查看:65
本文介绍了RegEx:捕获字符串中的引号之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下内容有关: RegEx:在引号之间获取值.

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

如果有这样的字符串:

HYPERLINK "hyperlink_funda.docx" \l "Sales"

链接上给出的正则表达式

The regex given on the link

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

在给我

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

什么正则表达式将返回用引号引起来的值(特别是在\"标记之间)?

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

["hyperlink_funda.docx", "Sales"]

String.split(String regex)方式使用Java.

推荐答案

您不应将其与.split()方法一起使用.而是将 Pattern 与捕获组:

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));
}

输出:

hyperlink_funda.docx
销售

hyperlink_funda.docx
Sales

这是 regex演示,这里是 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆