Agustin Seifert
Agustin Seifert

Reputation: 1968

Exclude some words with regexp

I have the text:

text text text text [text] text text -.[text1] -.[text2]

I want to extract only the words with only brakets [], exluding words with -.[

for this example i want only [text] and no -.[text1] and -.[text2]

Ty!

Upvotes: 0

Views: 105

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208415

The following should work, assuming the language or library you are using supports lookbehind:

(?<!-\.)\[[^\]]*\]

Explanation:

(?<!-\.)    # fail if the previous characters are '-.'
\[          # match a literal '['
[^\]]*      # match any number of characters that are not ']'
\]          # match a literal ']'

Example: http://www.rubular.com/r/HqdR3tZy9R

Upvotes: 2

Related Questions