admin管理员组文章数量:1296401
I want to do string templating with py parsing. I have failed multiple times to define a grammar that can parse a string with inserted text inbetween "#{" and "}" (seems pretty basic) :
Example of grammar which doesn't work :
from pyparsing import CharsNotIn, FollowedBy, Group, Literal, OneOrMore, Suppress
# Define the unwanted sequence
hash_tag = Suppress(Literal('#{')) + CharsNotIn('}') + Suppress(Literal('}'))
text_part = CharsNotIn('#{')
# Combine the parts to match the entire string
parser = OneOrMore(Group(hash_tag) | text_part)
test_string = "Some text \n with a stranded # and a stranded { then a correct #{ insertion of \n some # other text } and then some text"
result = parser.searchString(test_string)
expected_result = ["Some text \n with a stranded # and a stranded { then a correct ",[" insertion of \n some # other text "]," and then some text"]
print(result)
The result is not the expected result (I get ["Some text \n with a stranded "])
I want to do string templating with py parsing. I have failed multiple times to define a grammar that can parse a string with inserted text inbetween "#{" and "}" (seems pretty basic) :
Example of grammar which doesn't work :
from pyparsing import CharsNotIn, FollowedBy, Group, Literal, OneOrMore, Suppress
# Define the unwanted sequence
hash_tag = Suppress(Literal('#{')) + CharsNotIn('}') + Suppress(Literal('}'))
text_part = CharsNotIn('#{')
# Combine the parts to match the entire string
parser = OneOrMore(Group(hash_tag) | text_part)
test_string = "Some text \n with a stranded # and a stranded { then a correct #{ insertion of \n some # other text } and then some text"
result = parser.searchString(test_string)
expected_result = ["Some text \n with a stranded # and a stranded { then a correct ",[" insertion of \n some # other text "]," and then some text"]
print(result)
The result is not the expected result (I get ["Some text \n with a stranded "])
Share Improve this question edited Feb 11 at 22:48 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Feb 11 at 19:19 FoguxFogux 1279 bronze badges 1 |1 Answer
Reset to default 0I would use pyparsing's QuotedString
class to define your template field, like this:
import pyparsing as pp
template_field = pp.QuotedString("#{", endQuoteChar="}",
escChar="\\",
multiline=True,
unquoteResults=True)
test_string = "Some text \n with a stranded # and a stranded { then a correct #{ insertion of \n some # other text } and then some text"
# just search for the template field
result = template_field.search_string(test_string)
expected_result = [[" insertion of \n some # other text "]]
print(result)
print(expected_result)
Since this is a templating kind of program, transform_string might do much of the search-and-replace work for you, if you can implement the transformation as a parse action:
# Using transform_string to transform the template_field
# (add some kind of transformation to the template_field using a parse action)
template_field.add_parse_action(lambda s, l, t: t[0].upper())
print(template_field.transform_string(test_string))
本文标签: pythonHow to a match any sequence of text until quotquot in pyparsingStack Overflow
版权声明:本文标题:python - How to a match any sequence of text until "#{" in pyparsing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639261a2389820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
transform_string
method instead ofsearch_string
. Themacro_expander.py
script in the examples directory might give you some ideas also.. – PaulMcG Commented Feb 20 at 6:28