Using Text::Balanced to find strings enclosed in fat brackets {{..}} or [[..]]

67 Views Asked by At

I want to use Text::Balanced to extract some markup that uses {{...}} and [[...]] to markup its hyperlinks. For example:

This line contains {{path:to:mediafile|a hyperlink}} and [[path:to:page|another]] too.

extract_bracketed() is smart in the sense that it knows about bracket pairs --{}, [], <>, ()-- and looks for opening and closing brackets that are the same "shape". I can't use it, though, because it only works with single-character brackets.

I can use extract_tagged() to find one such type of "fat bracket" at a time, but it would be nice if it understood several symmetric bracket pairs. Is there a way to use Text::Balanced to extract strings that are enclosed by either {{ and }}, or [[ and ]], but not {{ and ]], for example?

1

There are 1 best solutions below

1
On BEST ANSWER

Since they can't be nested, it can be done rather easily without Text::Balanced.

s/
   \{\{ ( (?:(?!\}\}).)* ) \}\}
   |
   \[\[ ( (?:(?!\]\]).)* ) \]\]
/
   defined($1) ? handle_curlies($1) : handle_squares($2)
/sexg