tags:

views:

60

answers:

3

I am using 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: 

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).

Bart Kiers
+2  A: 

A regex of \[Blah[^\]]*\] is the usual way. It means:

  • literal string [Blah
  • zero or more:
    • 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).

Roger Pate
You don't actually need to escape the `]` in the character class, if you really want to be evil :)
Porges
A: 

Try changing it to \[Blah[^\]]*\] or \[Blah.*?\]. They should give the same result, but there might be a difference in their performance.

Aleksander Kmetec