开发者

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 alte开发者_StackOverflowrnative):

([-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?


This should work:

-?([^-]-?)*

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜