开发者

Ensure string start and end can be matched, otherwise fail regex

I have this regex:

{link=([^|{}]+)|([^|{}]+)|([^|{}]+)}

this works great and returns me three groups between the pipe symbols, ie. this, that and blah from {link=this|that|blah}. As long as the text contains no pipe, or two curly braces (as they are reserved words in my tag builder.)

However I am still getting a match if I am testing the string 'fsdjklrwenklw' or anything for that matter, I think it's because it doesn't care if "{link=" matches or not, as any string will then be matched in the very first back reference.

Probably terribly written, could somebody help me out? I must only get back references for strings that start with "{link=" and 开发者_JS百科end with "}".

PS. I have tried the simple prefix and suffix of \A \z, this does not help.

Thanks.


You need to escape the | characters.

Your regex is being parsed as {link=([^|{}]+) | ([^|{}]+) | ([^|{}]+)}. The | characters mean 'or', so it matches either the first part or the second part o the first part.

You need to write {link=([^|{}]+)\|([^|{}]+)\|([^|{}]+)}.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜