开发者

Regular expression circular matching

Using regular expressions (in any language) is ther开发者_JS百科e a way to match a pattern that wraps around from the end to the beginning of a string? For example, if i want the match the pattern:

"street"

against the string:

m = "et stre"

it would match m[3:] + m[:2]


You can't do that directly in the regexp. What you can do is some arithmetic. Append the string to itself:

m = "et stre"
n = m + m //n = "et street stre"

If there is an odd number of matches in n (in this case, 1), the match was 'circular'. If not, there were no circular matches, and the number of matches in n is the double of the number of matches in m.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜