C# Regex Split - Everything Inside Square Brackets

Regex.Split everything inside square brackets []

The regex you want to use is:

Regex.Split( s, "\\[.*?\\]" );

Square brackets are special characters (specifying a character group), so they have to be escaped with a backslash. Inside the square brackets, you want any sequence of characters EXCEPT a close square bracket. There are a couple of ways to handle that. One is to specify [^\]]* (explicitly specifying "not a close square bracket"). The other, as I used in my answer, is to specify that the match is not greedy by affixing a question mark after it. This tells the regular expression processor not to greedily consume as many characters as it can, but to stop as soon as the next expression is matched.

C# Regex Split - everything inside square brackets

Split won't help you here; you need to use regular expressions:

// using System.Text.RegularExpressions;
// pattern = any number of arbitrary characters between square brackets.
var pattern = @"\[(.*?)\]";
var query = "H1-receptor antagonist [HSA:3269] [PATH:hsa04080(3269)]";
var matches = Regex.Matches(query, pattern);

foreach (Match m in matches) {
Console.WriteLine(m.Groups[1]);
}

Yields your results.

Regex to match everything inside brackets ignore nested

You can use

\[(?>[^][]+|(?<c>)\[|(?<-c>)])+]

See the regex demo. Details:

  • \[ - a [ char
  • (?>[^][]+|(?<c>)\[|(?<-c>)])+ - one or more occurrences of
    • [^][]+| - one or more chars other than ] and [, or
    • (?<c>)\[| - a [ char and a value is pushed onto Group "c" stack, or
    • (?<-c>)] - a ] char and a value is popped off the Group "c" stack
  • ] - a ] char.

How separate words by excluding square brackets in C# using regex

Assuming you want the fragments before and after the brackets as separate entries in a collection:

Regex.Split(avatarVM.AvatarText, @"\[[^\]]+\]");

This will also work if there are multiple bracketed fragments in the string. You may want to .Trim() each entry.

Splitting a string in C# to extract values in parentheses and keep them

You can use Regex.Matches method instead

        string phrase = "123(0)(1)";
string[] results = Regex.Matches(phrase, @"\(.*?\)").Cast<Match>().Select(m => m.Value).ToArray();

Regex to split and ignore brackets

You can use

string pattern = @"(?:""[^""]*""|\([^()]*\)|[^,])+";
string input = "Selectroasted peanuts,Sugars (sugar, fancymolasses),Hydrogenatedvegetable oil (cottonseed and rapeseed oil),Salt.";
foreach (Match m in Regex.Matches(input.TrimEnd(new[] {'!', '?', '.', '…'}), pattern))
{
Console.WriteLine("{0}", m.Value);
}
// => Selectroasted peanuts
// Sugars (sugar, fancymolasses)
// Hydrogenatedvegetable oil (cottonseed and rapeseed oil)
// Salt

See the C# demo. See the regex demo, too. It matches one or more occurrences of

  • "[^"]*" - ", zero or more chars other than " and then a "
  • | - or
  • \([^()]*\) - a (, then any zero or more chars other than ( and ) and then a ) char
  • | - or
  • [^,] - a char other than a ,.

Note the .TrimEnd(new[] {'!', '?', '.', '…'}) part in the code snippet is meant to remove the trailing sentence punctuation, but if you can affort Salt. in the output, you can remove that part.

Regular expression to extract text between square brackets

You can use the following regex globally:

\[(.*?)\]

Explanation:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • (.*?) : match everything in a non-greedy way and capture it.
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.


Related Topics



Leave a reply



Submit