Nk Mk
Nk Mk

Reputation: 1079

how to match dot underscore underscore ".__"

I'm trying to replace all occurences of .__ with __ and .- with - so the following:

article.__authors.__item ...would become... article__authors__item

...and...

bucket.__water.-half ...would become... bucket__water-half

I started with the .__ part and came up with \b\.__\b as my regex but http://gskinner.com/RegExr/ doesn't seem to like it...

This works for the 2nd match though \b\.-\b

Is there a better way?

Cheers, Nick

Upvotes: 2

Views: 713

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

So what you actually want to do is to remove a dot that is preceded by an alphanumeric character and followed by either a dash or two underscores? Then you can do this using positive lookahead assertions:

Search for \b\.(?=__|-) and replace with the empty string.

If you also want to make sure that another alphanumeric letter follows after the underscores/dash, you can:

Search for \b\.(?=__(?=[A-Za-z0-9])|-\b) and replace with the empty string.

Upvotes: 2

I'd remove the \bs unless you want your regex to only match where there is a word boundary.

AS your regex is right now, it would find the pattern in article.__ authors but not in article.__authors because there is no \b between _ and a

It would also fail to find the patern in .__ authors because there is no \b before the period.

\.__ would work just fine

The reason it works with your hyphen, is because _ is considered a legal character for a word but a - isn't. so -a has a word boundary before a, but _a does not

http://rubular.com/r/cgT1bD7o0d

Upvotes: 6

Related Questions