Domanda

I may parse my_str with following regex code:

([\w\s]*)\s(\w+)

but I want to use pyparsing.

How can I do that?

my_str = "aa234"
expected_result = ["aa234", ""]

my_str = "aa234 bbb2b ccc ddd eee"
expected_result = ["aa234 bbb2b ccc ddd", "eee"]


my_str = "aa234 bbb2b ccc ddd eee fff ggg hhh"
expected_result = ["aa234 bbb2b ccc ddd eee fff ggg", "hhh"]
È stato utile?

Soluzione

Here is your sample parser:

from pyparsing import *

stringWord = Word(alphas, alphanums)

# only want words not at the end of the string for the leading part
leadingWord = stringWord + ~LineEnd()

leadingPart = originalTextFor(stringWord + ZeroOrMore(leadingWord))

# define parser, with named results, similar to named groups in a regex
parser = leadingPart("first") + Optional(stringWord, default='')("second")

Here's how it works in practice:

tests = ["aa234", 
         "aa234 bbb2b ccc ddd eee ",]
for test in tests:
    results = parser.parseString(test)
    print results.dump()
    print results.first
    print results.second

Prints:

['aa234', '']
- first: aa234
- second: 
aa234

['aa234 bbb2b ccc ddd', 'eee']
- first: aa234 bbb2b ccc ddd
- second: eee
aa234 bbb2b ccc ddd
eee
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top