Reputation: 23
With regular expression I want to detect text/string between starting and ending double curly braces and it should detect any inner curly braces along with text.
for example:
{{detect this {{and this as well}} text}} but text does not ends here so it should {{not detect this}}.
I have written this regexp
\{\{[\s\S]+\}\}
but this selects the whole string FROM {{detect this.... TO {{not detect this}}
Note: I am using python re for this
Upvotes: 2
Views: 921
Reputation: 63749
Pyparsing allows you to define recursive grammars, but has some builtin helpers for common ones like this. See annotated code sample below:
from pyparsing import nestedExpr, ungroup, originalTextFor
# use nestedExpr to define a default expression with left-right nesting markers
nestedText = ungroup(nestedExpr('{{','}}'))
sample = """{{detect this {{and this as well}} text}} but text does not ends here so it should {{not detect this}}."""
# note how reporting the results as a list keeps the nesting of {{ }}'s
print nestedText.parseString(sample).asList()
# prints ['detect', 'this', ['and', 'this', 'as', 'well'], 'text']
# if you just want the string itself, wrap with 'originalTextFor'
print originalTextFor(nestedText).parseString(sample)[0]
# prints {{detect this {{and this as well}} text}}
Upvotes: 3
Reputation: 399
First of all {{[\s\S]+}}
is (almost) the same as {{.+}}
.
Reason: \s
contains all spaces and \S
contains everything that is not a space.
I would generally avoid the upper case character classes in []
, it will mostly cause confusion.
Secondly: I think I am on board with thefourtheye, I cannot quickly think of a RegEx to solve your problem.
Upvotes: 0