繁体   English   中英

如何使用Regex获取包含连续方括号中元素的字符串的子串?

[英]How to Get Substring of a string having elements in Consecutive Square Brackets using Regex?

我有以下字符串:一个规则:获取所有连续的方括号字符串:例如,

string 1:[hello] [qwqwe:] sdsdfsdf [note2]
string 2:[somethingelse] sdfsdf [note 1]
string 3:aasdad [note 3]

我想得到子串:

输出1:[你好] [qwqwe:]
输出2:[somethingelse]
输出3:

如果字符串没有方括号,我不想输出。 如果字符串有一个方括号分隔的字符串,它不是连续的,它也不应该匹配。

我尝试使用正则表达式

([*])*

但它匹配两个方括号之间的所有内容。 如果您注意到第一个字符串,我不需要违反我的规则的字符串部分。

方法1:匹配多个连续的[...]在字符串s开始作为一个字符串

您需要使用以下正则表达式:

^(\[[^]]*])+

请参阅正则表达式演示

^(\\[[^]]*])+匹配:

  • ^ - 字符串的开头(在演示中,由于多线修改器,它在行开始时匹配)
  • (\\[[^]]*])+ - 捕获到组1(您可以通过.Groups[1].Captures集合访问所有这些值)一次或多次出现...
    • \\[ - 文字[
    • [^]]* -比其他零个或多个字符]
    • ] - 文字]

C#代码演示

var txt = "[hello][qwqwe:]sdsdfsdf [note2]";
var res = Regex.Match(txt, @"^(\[[^]]*])+"); // Run the single search
Console.WriteLine(res.Value); // Display the match
var captures = res.Groups[1].Captures.Cast<Capture>().Select(p => p.Value).ToList();
Console.WriteLine(string.Join(", ", captures)); // Display captures

方法2: 分别在字符串开始时匹配多个连续的[...] s

你可以使用\\G运算符:

\G\[[^]]*]

请参阅正则表达式演示

它将匹配[...]在字符串的开始,然后经过每个成功匹配的子串。

正则表达式解释

  • \\G - 与字符串开头的位置匹配的零宽度断言(锚点),或者在每次成功匹配后匹配
  • \\[[^]]*] -文字[\\[ ),然后加入更多的零( * )大于其他字符] ,接着闭合]

如果需要返回所有的单个字符串[...]作者发现在字符串的开头,你需要连接的比赛:

var txt = "[hello][qwqwe:]sdsdfsdf [note2]";
var res = Regex.Matches(txt, @"\G\[[^]]*]").Cast<Match>().Select(p => p.Value).ToList();
Console.WriteLine(string.Join("", res));

请参阅IDEONE演示

你可以使用这个正则表达式。

^(\[(.*?)\])*

用于匹配的C#代码:

        var regex = new Regex(@"^(\[(.*?)\])*");

        var inputTexts = new string [] {"[abcd]xyz[pqrst]","abcd[xyz][pqr]","[asdf][abcd][qwer]sds[qwert]" };

        foreach (var match in inputTexts.Select(inputText => regex.Match(inputText)))
        {
            Console.WriteLine(match.Value);
        }           

        //result1 - [abcd]
        //result2 -
        //result3 - [asdf][abcd][qwer]

您可以从原始正则表达式中调整四项内容以使其正常工作:1)使用非贪婪匹配.*? ,2)添加^以匹配字符串的开头,3)转义方括号,以及4)将final *更改为+以至少需要一组方括号:

^(\[.*?\])+

试试这个,它适用于我的测试字符串。

^(\[[^\]]*(\]|\]\[))*

https://regex101.com/生成的说明:

1st Capturing group (\[[^\]]*(\]|\]\[))*
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
\[ matches the character [ literally
[^\]]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\] matches the character ] literally
2nd Capturing group (\]|\]\[)
1st Alternative: \]
\] matches the character ] literally
2nd Alternative: \]\[
\] matches the character ] literally
\[ matches the character [ literally

暂无
暂无

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

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