Valour
Valour

Reputation: 810

How to exlude certain word on regex

I have a text document that I need to modify. Most of the words are seperated by "-" (minus) character.

So in sublime text, I tried this pattern:

(\w+)\-(\w+)

This pattern works perfectly fine but there is one word that has "-" (minus) character naturally in the document. (Eg: foo-bar)

So I need a pattern that finds all minus seperated words but exludes "foo-bar"

Sorry if this question asked before but I couldn't find the answer I needed

Upvotes: 0

Views: 49

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627086

You can use a negative look-ahead (with optional i switch to match words in a case-insensitive way):

(?i)(?!\bfoo\-bar\b)\b(\w+)-(\w+)\b

Mind that this will only work with non-overlapping matches.

See example:

enter image description here

If you want to replace a hyphen with space in cases I provided in the screenshot, you can use (?!\bfoo\-bar\b)\b(\w+)\-(?=\w) search regex and replace with $1 (result: go there now):

enter image description here

Upvotes: 1

Related Questions