tags:

views:

28

answers:

1

Hey there,

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 end with "}".

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

Thanks.

+2  A: 

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

SLaks
Ahhhhhh crap! Will test. Thank you =)
GONeale
I already tried it.
SLaks
Thanks, yes that part is working now. Do you know how I could make each backreference group optional, but having at least one? As in, either {link=test}, {link=test|test} or {link=test|test|test} ?
GONeale
I might have a go at adding another pipe and trying that. I think I intended to use that pipe for OR, but actually wanted to test a pipe string as well.
GONeale
Sorry I'm stuck on my second requirement, if you have any ideas please let me know.
GONeale