Jikku Jose
Jikku Jose

Reputation: 18804

Regex for matching some characters that should be left out later

I will try to explain my situation with an example, consider the following string:

03 - The-Basics-of-Querying-the-Dom.mov

I need to remove all -s (hyphens) excluding the one after the digits. In other words, all hyphens in between the words.

This is the REGEX I created: /([^\s])\-/. But the problem is, when I try to replace, the character before the space is also removed.

Following the result I am aiming for:

03 - The Basics of Querying the Dom.mov

Think, I can use something like exclude groups? I tried to use ?: & ?! in the capture group to avoid it from being matched, but didn't give any positive results.

Upvotes: 0

Views: 43

Answers (2)

revo
revo

Reputation: 48711

I just modified your already proposed RegEx by using a positive lookbehind (which only asserts the correct position):

/(?<=[^\s])\-/

Upvotes: 0

heemayl
heemayl

Reputation: 42017

You can do:

(?<=\w)-(?=\w)

Demo

Upvotes: 1

Related Questions