Wie Sie eine Abfolge von Text bis "#{" in Pyparsing übereinstimmenPython

Python-Programme
Anonymous
 Wie Sie eine Abfolge von Text bis "#{" in Pyparsing übereinstimmen

Post by Anonymous »

Ich möchte eine String -Vorlagen mit PY -Parsen durchführen. /p>
Beispiel für Grammatik, das nicht funktioniert: < /p>

Code: Select all

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)
Das Ergebnis ist nicht das erwartete Ergebnis (ich erhalte ["Einige Text \ n mit einem gestrandeten"])

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post