regular expression to detect text in between double curly braces

折月煮酒 提交于 2021-02-08 03:40:07

问题


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


回答1:


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}}



回答2:


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.



来源:https://stackoverflow.com/questions/20936622/regular-expression-to-detect-text-in-between-double-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!