RegEx:捕获字符串中的引号之间的值 [英] RegEx: Grabbing value between quotation marks from string
本文介绍了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