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=([^|{}]+)\|([^|{}]+)\|([^|{}]+)}
.
精彩评论