简体   繁体   English

正则表达式 - 匹配括号之间的引号

[英]Regex - Match quotes between brackets

Example string:示例字符串:

cov('Age', ['5','7','9']) cov('年龄', ['5','7','9'])

I have this RegEx that matches the values inside quotes:我有这个与引号内的值匹配的正则表达式:

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

I´m trying to modifiy it to only return the quotes inside the square brackets from the example string using lookahead/lookbehind:我正在尝试将其修改为仅使用前瞻/后视从示例字符串返回方括号内的引号:

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

But it matches everything inside the square brackets.但它匹配方括号内的所有内容。

How can i match only the quotes without the commas like in the first regex, but inside the square brackets?我怎样才能只匹配没有逗号的引号,就像第一个正则表达式一样,但在方括号内?

Edit.编辑。

The language is .NET.语言是 .NET。

One option if supported is to make use of the \G anchor and a capturing group:如果支持,一个选项是使用\G锚和捕获组:

(?:\[|\G(?!^))('[^']+'),?(?=[^\]]*\])

In parts在零件

  • (?: Non capturing group (?:非捕获组
  • \[ Match opening [ \[比赛开场[
    • | Or或者
    • \G(?!^) Assert position at the end of the previous match \G(?!^)在上一场比赛结束时断言 position
  • ) Close non capturing group )关闭非捕获组
  • ( Capture group 1 (捕获组 1
    • '[^']+' Match ' , 1+ times any char except ' , then match ' again '[^']+'匹配' ,1+ 次除'之外的任何字符,然后再次匹配'
  • ) Close group 1 )关闭第 1 组
  • ,? Match an optional ,匹配一个可选的,
  • (?=[^\]]*\]) Positive lookahead, assert a closing ] (?=[^\]]*\])正向前瞻,断言结束]

Regex demo |正则表达式演示| C# demo C# 演示

For example例如

string pattern = @"(?:\[|\G(?!^))('[^']+'),?(?=[^\]]*\])";
string input = @"cov('Age', ['5','7','9'])";

var results = Regex.Matches(input, pattern)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToArray();

foreach(string result in results)
{
    Console.WriteLine(result);
}

Output Output

'5'
'7'
'9'

You haven't specified a language or regex engine so answering your question is difficult.您尚未指定语言或正则表达式引擎,因此很难回答您的问题。 The fourth bird's answer works for specific regex engines (eg PCRE) but not others.第四只鸟的答案适用于特定的正则表达式引擎(例如 PCRE),但不适用于其他引擎。 Another alternative exists in .NET as well. .NET 中也存在另一种选择。

For you can use the following since this regex engine collects all captures into a CaptureCollection :对于您可以使用以下内容,因为此正则表达式引擎将所有捕获收集到CaptureCollection中:

See regex in use here 请参阅此处使用的正则表达式

\[('[^']*'[,\]])+(?<=])

For most other languages (not covered by this answer or @Thefourthbird's ), you'll want to do this in two steps:对于大多数其他语言(此答案或@Thefourthbird's未涵盖),您需要分两步执行此操作:

  • Get all strings that match \[([^[\]]*)] (you want the value of group 1)获取所有匹配\[([^[\]]*)]的字符串(你想要第 1 组的值)
  • Match all occurrences of '([^']*)' (you want the value of group 1 for contents)匹配所有出现的'([^']*)' (您需要第 1 组的值作为内容)

You do not seem to need any complex regex: grab a string between two square brackets and split the captured contents with single quote or comma, or plainly match what you need there.您似乎不需要任何复杂的正则表达式:在两个方括号之间抓取一个字符串,并用单引号或逗号分隔捕获的内容,或者在此处明确匹配您需要的内容。

Given给定

var text = "cov('Age', ['5','7','9'])";

The approaches can be:方法可以是:

// Split captured text with ' and , 
var results = Regex.Matches(text, @"\[([^][]+)]")
        .Cast<Match>()
        .Select(x => x.Groups[1].Value.Split('\'', ',').Where(c => !string.IsNullOrEmpty(c)));

Or, match the strings between brackets and then extract all 1+ digit chunks from it:或者,匹配括号之间的字符串,然后从中提取所有 1+ 数字块:

var results1 = Regex.Matches(text, @"\[([^][]+)]")
        .Cast<Match>()
        .Select(x => Regex.Matches(x.Groups[1].Value, @"\d+"));

Or, just extract all numbers inside [...] :或者,只需提取[...]中的所有数字:

var results = Regex.Matches(text, @"(?<=\[[^][]*)\d+(?=[^][]*])").Cast<Match>().Select(x => x.Value);

Here, the regex matches在这里, 正则表达式匹配

  • (?<=\[[^][]*) - position that is preceded with [ and any amount of chars other than [ and ] (?<=\[[^][]*) - position 前面有[和除[]以外的任何数量的字符
  • \d+ - 1+ digits \d+ - 1+ 位
  • (?=[^][]*]) - position followed with any 0+ chars other than [ and ] and then ] . (?=[^][]*]) - position 后跟除[]之外的任何 0+ 字符,然后是]

See the online C# demo .请参阅在线 C# 演示

It gets a bit more complex to extract any number , replace \d+ with [-+]?\d*\.?\d+([eE][-+]?\d+)?提取任何数字变得有点复杂,将\d+替换为[-+]?\d*\.?\d+([eE][-+]?\d+)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM