tags:

views:

45

answers:

2

I am not too familiar with regex and hope someone could help. example:

This is a sentence with some_unicode[some other word] and other stuff.

After removing the characters and brackets, the result should be:

This is a sentence with and other stuff.

Thank you!!

+2  A: 

[ and ] are metacharacters in regular expressions and must be escaped by a backslash, e.g. \[.

Ignacio Vazquez-Abrams
Thank you for the response!
Unikorn
+3  A: 

Search for

some_unicode\[[^\]]*\]

and replace with nothing.

Explanation:

\[: Match a literal [.

[: Match a character class with the following properties (here [ is a metacharacter, starting a character class)...

^\]: "any character except a literal ]" (^ at the start of a character class negates its contents).

]*: ...zero or more times. Note again the unescaped ], ending the character class.

\]: Match a literal ].

This of course will only work if there can be no brackets inside brackets. How to actually format and use the regex is highly dependent on the language/tool you're doing this with; so if you add another tag to your question specifying the language, I can give you a code example.

Tim Pietzcker
Thanks! I ended up with something like this... \\[.*\\] seems to work.
Unikorn
This will work as long as there is exactly one pair of brackets in your input. As soon as there are more, you'll end up matching text outside of brackets, too. The negated character class in my regex prevents this from happening. Also, it is more effective since you regex will first match everything from the first bracket to the end of the entire string, and then backtrack character by character until it finally reaches the closing bracket.
Tim Pietzcker