Tomáš Zato
Tomáš Zato

Reputation: 53129

Exclude characters from [ ] group regex, while still looking for characters

I want to match all the words that do not contain letter l. I tried this:

[a-z^k]+

But apparently ^ only works right behind [. If it was just letter l, I guess this would do:

[a-km-z]+

Of course apart from the fact that it just treats l-words as two words:

image description

But this is not the real concern, the question remains just as in title:

Q: How do I search for list of characters, but exclude another list of characters?

Upvotes: 4

Views: 11919

Answers (1)

vks
vks

Reputation: 67968

You need to use \b word boundary to make sure that the match doesn't start and end within words.

\b[a-km-z]+\b

Alternatively, you can create an exclusion list using lookahead.

\b(?:(?![l])[a-z])+\b

Demo on regex101

Upvotes: 8

Related Questions