Given the following strin:
var s = "my [first] ga[m]e and [abc]de]\nThe [Game]"
Which regex do I use to match:
0 - [first]
1 - [abc]de]
2 - [Game]
I have tried var pattern2 = new Regex(@"\\W\\[.*?\\]\\W");
but it doesn't find [Game]
I also want to be able to match " [my] gamers\\t[pro]
"
0 - [my]
1 - [pro]
\[[^\[]{2,}\]
Explanation:
\[ # Match a [
[^\[]{2,} # Match two or more non-[ characters
\] # Match ]
See it on RegExr .
You need to match beginning and end of the string explicitly, in addition to non-word characters:
(?:^|\W)\[(.*?)\](?:$|\W)
The capture group will get the words inside the brackets.
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.