Smalltalkman
Smalltalkman

Reputation: 33

regex skip embedded patterns

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.

Upvotes: 2

Views: 87

Answers (1)

Martin Ender
Martin Ender

Reputation: 44279

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.

Upvotes: 1

Related Questions