regex to strip out [blah: ... ] tag from string
I am usin开发者_开发百科g this regex:
[Blah(?:\s*)\]
I want to strip out the tag that looks like:
[Blah:http:..anyting goes here so catch all types of characters ]
Any tips on what's wrong with my regex?
A regex of \[Blah[^\]]*\]
is the usual way. It means:
- literal string
[Blah
- zero or more:
- characters that aren't
]
- characters that aren't
- literal string
]
If you want to handle nesting (e.g. input of the form [a[b[c]]]
), then you need something other than regex (this is one reason why trying to use regex to parse HTML doesn't work).
Your regex [Blah(?:\s*)\]
starts with an unescaped '[' which is "seen" as the start of a character class. That's what's wrong with your regex (there are probably more errors, but that one is the main reason).
Try changing it to \[Blah[^\]]*\]
or \[Blah.*?\]
. They should give the same result, but there might be a difference in their performance.
精彩评论