簡體   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