Parse is a text processing toolkit in Python, including FindAll (), parse (), format () , and so on, findall () more commonly used (for me), format (), Parse () is inverse to each other.
Take a look at the usage of parse () : (code excerpt from document)
1>>> fromParseImport*2 3>>> Parse ("It ' s {}, I love it!","It ' s spam, I love it!")4<result ('spam',) {}>5>>>_[0]6 'spam'7 8>>> r = Parse ("The {} who say {}","The Knights who say ni!")9>>>Print(R)Ten<result ('Knights','ni!') {}> One A #dotted names is possible though the application must make additional sense of the result: ->>> r = Parse ("Mmm, {food.type}, I love it!","Mmm, spam, I love it!") ->>>Print(R) the<result () {'Food.type':'spam'}> ->>>Print(r.named) -{'Food.type':'spam'} ->>>Print(r['Food.type']) + spam - + A>>>"'. Join (R.fixed[0] forRinchFindAll">{}<","<p>the <b>bold</b> text</p>")) at 'The bold text' - - - #If you ' re going to use the same pattern to match lots of strings you can compile it once: - ->>> fromParseImportCompile in>>> p = Compile ("It ' s {}, I love it!") ->>>Print(P) to<parser"It ' s {}, I love it!"> +>>> P.parse ("It ' s spam, I love it!") -<result ('spam',) {}> the * $ ##use Format SpecificationPanax Notoginseng>>> Search ('Age : {:d}\n','name:rufus\nage:42\ncolor:red\n') -<result ({}>) the + #Would return None if typing does not match A>>> Parse ('Our {:d} {: W} is ...','Our 3 weapons is ...') the<result (3,'weapons') {}> + ->>> Parse ('Our {:d} {: W} is ...','Our three weapons is ...') $ #None $ ->>> Parse ('Meet at {: TG}','Meet at 1/2/2011 11:00am') -<result (Datetime.datetime (2, 1, 0),) {}> the - #AlignmentWuyi>>> Parse ('With {:} herring','With a herring') the<result ('a',) {}> ->>> Parse ('Spam {: ^} spam','spam lovely spam') Wu<result ('Lovely',) {}> - About #Use a dictionary to do some type-convert $>>>defshouty (String): -...returnString.upper () - ... ->>> Parse ('{: shouty} World','Hello World', Dict (shouty=shouty)) A<result ('HELLO',) {}> + the #If the type converter has the pattern attribute ->>>defparse_number (text): $...returnint (text) the>>> Parse_number.pattern = r'\d+' the>>> Parse ('Answer: {number:number}','answer:42', Dict (number=parse_number)) the<result () {' Number': 42}> the>>> _ = Parse ('Answer: {: number}','Answer:alice', Dict (number=parse_number)) ->>>assert_ isNone,"mismatch" in the the #Use With_pattern (pattern) Decorator About>>> fromParseImportWith_pattern the>>> @with_pattern (r'\d+') the...defparse_number (text): the...returnint (text) +>>> Parse ('Answer: {number:number}','answer:42', Dict (number=parse_number)) -<result () {' Number': 42}>
The Python tricks series uses parse to process text