Use regular expressions in python to search for nested string groups.

Source: Internet
Author: User

Use regular expressions in python to search for nested string groups.

If you see a small requirement on the internet, you need to use a regular expression to handle it. The original requirements are as follows:

Find the text that contains "because ...... Therefore, the sentence is centered on the two words and the three words before and after the output are fully output in the middle. If "because" and "so" still exist in the middle, "because" and "so ", you also need to find out and calculate another line. The output format is as follows:

The first three characters in the row number * Because * All & so & the last three characters (punctuation is counted as one word)

2 not yet * Because * this is good, & so & no one

The implementation method is as follows:

# Encoding: utf-8import osimport redef getPairStriList (filename): pairStrList = [] textFile = open (filename, 'R') pattern = re. compile (U '. {3} \ u56e0 \ u4e3a. * \ u6240 \ u4ee5. {3} ') # U' \ u56e0 \ u4e3a and U' \ u6240 \ u4ee5' Are utf8 codes for line in textFile: utfLine = line. decode ('utf8') result = pattern. search (utfLine) while result: resultStr = result. group () pairStrList. append (resultStr) result = pattern. search (resultStr, 2, len (resultStr)-2) # convert the format of each string and splice it for I in range (len (pairStrList )): pairStrList [I] = pairStrList [I] [: 3] + pairStrList [I] [3: 5]. replace (U' \ u56e0 \ u4e3a ', U' * \ u56e0 \ u4e3a *', 1) + pairStrList [I] [5:] pairStrList [I] = pairStrList [I] [: len (pairStrList [I])-5] + pairStrList [I] [len (pairStrList [I])-5:]. replace (U' \ u6240 \ u4ee5 ', U' & \ u6240 \ u4ee5 &', 1) pairStrList [I] = str (I + 1) + ''+ pairStrList [I] return pairStrList if _ name _ = '_ main _': pairStrList = getPairStriList('test.txt ') for str in pairStrList: print

PS: Let's take a look at the group nesting using regular expressions in python.

Because the group itself is a complete regular expression, you can nest the group in other groups to build more complex expressions. The following is an example of group nesting:

# Python 3.6 # Cai junsheng # http://blog.csdn.net/caimouse/article/details/51749579 # import re def test_patterns (text, patterns): "" Given source text and a list of patterns, look for matches for each pattern within the text and print them to stdout. "# Look for each pattern in the text and print the results for pattern, desc in patterns: print ('{! R} ({}) \ n'. format (pattern, desc) print ('{! R }'. format (text) for match in re. finditer (pattern, text): s = match. start () e = match. end () prefix = ''* (s) print ('{}{! R }{}'. format (prefix, text [s: e], ''* (len (text)-e), end ='',) print (match. groups () if match. groupdict (): print ('{}{}'. format (''* (len (text)-s), match. groupdict (),) print () return

Example:

# Python 3.6 # Cai junsheng # http://blog.csdn.net/caimouse/article/details/51749579 # from re_test_patterns_groups import test_patterns ('ababbba ', [(r 'a (a *) (B *))', 'A followed by 0-n a and 0-n B ')],)

The output is as follows:

'a((a*)(b*))' (a followed by 0-n a and 0-n b) 'abbaabbba' 'abb'    ('bb', '', 'bb')   'aabbb'  ('abbb', 'a', 'bbb')     'a' ('', '', '')

Summary

The above section describes how to use regular expressions in python to search for nested string groups. I hope this will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.