buckley
buckley

Reputation: 14030

Matching parts of string that contain no consecutive dashes

I need a regex that will match strings of letters that do not contain two consecutive dashes.

I came close with this regex that uses lookaround (I see no alternative):

([-a-z](?<!--))+

Which given the following as input:

qsdsdqf--sqdfqsdfazer--azerzaer-azerzear

Produces three matches:

qsdsdqf-
sqdfqsdfazer-
azerzaer-azerzear

What I want however is:

qsdsdqf-
-sqdfqsdfazer-
-azerzaer-azerzear

So my regex loses the first dash, which I don't want.

Who can give me a hint or a regex that can do this?

Upvotes: 3

Views: 1101

Answers (2)

Alan Moore
Alan Moore

Reputation: 75222

Looks to me like you do want to match strings that contain double hyphens, but you want to break them into substrings that don't. Have you considered splitting it between pairs of hyphens? In other words, split on:

(?<=-)(?=-)

As for your regex, I think this is what you were getting at:

(?:[^-]+|-(?<!--)|\G-)+

The -(?<!--) will match one hyphen, but if the next character is also a hyphen the match ends. Next time around, \G- picks up the second hyphen because it's the next character; the only way that can happen (except at the beginning of the string) is if a previous match broke off at that point.

Be aware that this regex is more flavor dependent than most; I tested it in Java, but not all flavors support \G and lookbehinds.

Upvotes: 0

sth
sth

Reputation: 229563

This should work:

-?([^-]-?)*

It makes sure that there is at least one non-dash character between every two dashes.

Upvotes: 5

Related Questions