2

I am filtering some text with php. I look for patterns like:

Mary [qtip:had|past tense of have] little lamb.

I extract the anchor text "had" from the tool tip "past tense of have"]

part of the processing is to use the regular expression /\[qtip:([^\|\\]]+)\|?([^\\]]*)?\]/

this is working fine

I am trying to extend the functionality.

Mary [qtip:had|past [otherFunction:tense|verb form signalling time] of have] little lamb.

my simple minded pattern finds "had" & "past [otherFunction:tense|verb form indicating time"

I want a pattern which will skip embedded [] pairs. These are not allowed in anchor or tool tip.

1 Answer 1

1

Why do you escape the | and double escape the ]? And to solve your problem, simply disallow opening [ inside your pattern. That requires the pattern to reach the closing ] without encountering any nested square brackets.

preg_match_all('/\[qtip:([^|[\]]+)\|?([^[\]]*)\]/', $input, $matches);

I also removed the last ? since it is redundant if you use use * on the pattern that should become optional.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.