Python-regular expressions exercises (Core Python)

来源:互联网 发布:软件企业评估管理办法 编辑:程序博客网 时间:2024/06/06 02:53

      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

1

Q: Recognize the following strings: “bat,” “bit,” “but,” “hat,”“hit,” or “hut.”
A:

#! /usr/bin/env pythonimport repattern = '[bh][aiu]t'text = 'ddddd fhat'm = re.search(pattern, text)if m is not None: print m.group()

2

Q: Match any pair of words separated by a single space, that is, first and last names.
A:

import repattern = '[a-zA-Z]+ [a-zA-Z]+'text = 'John Lennon'm = re.match(pattern, text)if m is not None: print m.group()

3

Q: Match any word and single letter separated by a comma and single space, as in last name, first initial.
A:

#! /usr/bin/env pythonimport repattern = '([A-Z]\. )+[A-Za-z]+'text = 'W. J. Chun'm = re.match(pattern, text)if m is not None: print m.group()

4

Q: Match the set of all valid Python identifiers.
A:

#! /usr/bin/env pythonimport repattern = r'((?<=\s)[a-zA-Z_][\w_]+(?=\s+|$))|(^[a-zA-Z_][\w_]+(?=\s+|$))'text = 'var _variable v_ar3 5de_ *&@Jkla jkl$#@@'m = re.findall(pattern, text)result = []if m is not None:    for group in m:        for identity in group:            if identity is not '': result.append(identity)    print result

5

Q: Match a street address according to your local format (keep your regex general enough to match any number of street words, including the type designation). For example, American street addresses use the format: 1180 Bordeaux Drive. Make your regex flexible enough to support multi-word street names such as: 3120 De la Cruz Boulevard.
A:

#! /usr/bin/env pythonimport repattern = r'\d{4} ([a-zA-Z]+ )+[a-zA-Z]+'text = '3120 De la Cruz Boulevard'm = re.search(pattern, text)if m is not None: print m.group()

6

Q: Match simple Web domain names that begin with “www.”and end with a “.com” suffix; for example, www.yahoo.com. Extra Credit: If your regex also supports other high-level domain names, such as .edu, .net, etc. (for example, www.foothill.edu).
A:

#! /usr/bin/env pythonimport repattern = 'w{3}(\.[\w_]+)+((\.com)|(.edu)|(.net))'text = 'www.foothill.path.net'm = re.search(pattern, text)if m is not None: print m.group()

7

Q: Match the set of the string representations of all Python integers.
A:

#! /usr/bin/env pythonimport redef printInteger(match):    res = []    if match is not None:        for integers in match:            for integer in integers:                 if integer is not '': res.append(integer)        print reshexadecimal = r'((?<=\s)0[xX][0-9a-fA-F]+[lL]*(?=\s|$))|(^0[xX][0-9a-fA-F]+[lL]*(?=\s|$))'octal = r'((?<=\s)0[oO][0-8]+[lL]*(?=\s|$))|(^0[oO][0-8]+[lL]*(?=\s|$))'binary = r'((?<=\s)0b[01]+[lL]*(?=\s|$))|(^0b[01]+[lL]*(?=\s|$))'decimal = r'((?<=\s)[1-9][0-9]*[lL]*(?=\s|$))|(^[1-9][0-9]*[lL]*(?=\s|$))'text = 'djk 180 19.4 0X49 0b10101L 0O8721 0x72bfff 0xttcppa'm = re.findall(hexadecimal, text)if m is not None:     print 'Hexs: '    printInteger(m)m = re.findall(octal, text)if m is not None:     print 'Octals: '    printInteger(m)m = re.findall(binary, text)if m is not None:     print 'Binaries: '    printInteger(m)m = re.findall(decimal, text)if m is not None:     print 'Decimals: '    printInteger(m)
0 0
原创粉丝点击