简体   繁体   中英

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

I have the following strings: One Rule : Get all consecutive Square bracket strings : for example,

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

I would like to get the substrings :

output 1 : [hello][qwqwe:]
output 2 : [somethingelse]
output 3 :

If the string doesn't have square brackets, I do not want an output. If the string has a square bracket delimited string which is not consecutive, it should not match aswell.

I tried using the regex expression

([.*])*

But it matches everything between two square brackets. If you notice the first string, I do not need the part of the string that violates my rule.

Approach 1: Matching multiple consecutive [...] s at string start as a single string

You need to use the following regex:

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

See regex demo

The ^(\\[[^]]*])+ matches:

  • ^ - start of string (in the demo, it matches at line start due to the multiline modifier)
  • (\\[[^]]*])+ - captured into Group 1 (you can access all of those values via .Groups[1].Captures collection) one or more occurrences of...
    • \\[ - a literal [
    • [^]]* - zero or more characters other than ]
    • ] - a literal ] .

C# code demo :

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

Approach 2: Matching multiple consecutive [...] s at string start separately

You can use \\G operator:

\G\[[^]]*]

See regex demo

It will match the [...] substrings at the start of the string and then after each successful match.

Regex explanation :

  • \\G - a zero-width assertion (anchor) matching the location at the beginning of a string, or after each successful match
  • \\[[^]]*] - a literal [ ( \\[ ) followed by zero more ( * ) characters other than a ] , followed by a closing ] .

If you need to return a single string of all [...] s found at the beginning of the string, you need to concatenate the matches:

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

See IDEONE demo

you can use this regular expression.

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

C# code for matching:

        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]

You can adjust four things from you original regex to make it work: 1) use non-greedy match .*? , 2) add ^ to match from beginning of string, 3) escape the square brackets, and 4) change final * to + to require at least one group of square brackets:

^(\[.*?\])+

Try this, it works for me with your test strings.

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

Explanation generated by 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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