17

I need to find the regex for []

For eg, if the string is - Hi [Stack], Here is my [Tag] which i need to [Find].

It should return Stack, Tag, Find

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ankit
  • 6,388
  • 8
  • 54
  • 79
  • 2
    Can there be nested matches: `foo [bar[baz]] done` where you want to grab `bar[baz]`? Or perhaps `foo [bar\\]baz] done` where you want to grab `bar]baz`? Can there be new line(s) between `[` and `]`? – Bart Kiers Sep 09 '10 at 14:01

5 Answers5

52

Pretty simple, you just need to (1) escape the brackets with backslashes, and (2) use (.*?) to capture the contents.

\[(.*?)\]

The parentheses are a capturing group, they capture their contents for later use. The question mark after .* makes the matching non-greedy. This means it will match the shortest match possible, rather than the longest one. The difference between greedy and non-greedy comes up when you have multiple matches in a line:

Hi [Stack], Here is my [Tag] which i need to [Find].
   ^______________________________________________^

A greedy match will find the longest string possible between two sets of square brackets. That's not right. A non-greedy match will find the shortest:

Hi [Stack], Here is my [Tag] which i need to [Find].
   ^_____^

Anyways, the code will end up looking like:

string regex = @"\[(.*?)\]";
string text  = "Hi [Stack], Here is my [Tag] which i need to [Find].";

foreach (Match match in Regex.Matches(text, regex))
{
    Console.WriteLine("Found {0}", match.Groups[1].Value);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    @henning Yes, the equivalent Python code has the [same output](https://www.online-python.com/uO4ytT5qLN). – John Kugelman Nov 24 '21 at 17:11
  • "Pretty simple" not so much. Just put an ASCII character in your string see it yourself. For example, nothing will happen to this string. `"Hi [St" + (char) 10 +"ack]"` – Farid Dec 30 '21 at 08:49
  • @Farid Of course that won't work. `(char) 10` is a `\n` newline character. It has nothing to do with my answer. – John Kugelman Dec 30 '21 at 14:11
  • You are right it is a newline, though the question asks specifically "anything between". I think `\[(?s)(.*)\]` would be more accurate, at least, for future readers. – Farid Dec 31 '21 at 08:58
3
\[([\w]+?)\]

should work. You might have to change the matching group if you need to include special chars as well.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • This works a bit more nicely than the currently tagged answer because it has better control over things like this: "Test [test] [ test [test]" The tagged answer will include the stray [ in the match, whereas this regex will not. – StronglyTyped Aug 30 '12 at 13:14
3

Depending on what environment you mean:

\[([^\]]+)]
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
0

.NET syntax, taking care of multiple embedded brackets:

\[ ( (?: \\. | (?<OPEN> \[) | (?<-OPEN> \]) | [^\]] )*? (?(OPEN)(?!)) ) \]

This counts the number of opened [ sections in OPEN and only succeeds if OPEN is 0 in the end.

Mike
  • 843
  • 7
  • 13
0

I encountered a similar issue and discovered that this also does the trick.

\[\w{1,}\]

The \w means Metacharacter. This will match 1 or more word characters.

Using n{X,} quantifier matches any string where you can obtain different amounts. With the second number left out on purpose, the expression means 1 or more characters to match.