with 上下文管理器
difflib

Library Example - re

erhuabushuo posted @ 2012年9月17日 19:54 in Python , 1693 阅读

一、查找文本中的模式

#!/usr/bin/env python3
import re

pattern = "this"
text = "Does this text match the pattern?"

match = re.search(pattern, text)

s = match.start()
e = match.end()

print('Found "{0:s}"\nin "{1:s}"\nfrom {2:d} to {3:d} ("{4:s}")'.format(
     match.re.pattern, match.string, s, e, text[s:e]))

 

二、编译表达式

#!/usr/bin/env python3
import re

# Precompile the patterns
regexes = [ re.compile(p) 
            for p in ["this", "that"]
          ]

text = "Does this text match the pattern?"

print("Text: %r\n".format(text))

for regex in regexes:
    print("Seeking \"%s\" -> ".format(regex.pattern), end='')

    if regex.search(text):
        print("Match")
    else:
        print("No Match")

三、多重匹配

findall()函数返回输入中与模式匹配而不重叠的所有子串

#!/usr/bin/env python3
import re

text = "abbaaabbbaaaaa"
pattern = "ab"

for match in re.findall(pattern, text):
    print("Found \"{0}\"".format(match))

Found "ab"
Found "ab"

finditer()返回一个迭代器,它将生成Match实例

#!/usr/bin/env python3
import re

text = "abbaaabbbbaaaa"
pattern = "ab"

for match in re.finditer(pattern, text):
    s = match.start()
    e = match.end()
    print("Found \"{0}\" at {1:d}:{2:d}".format(text[s:e], s, e))

Found "ab" at 0:2
Found "ab" at 5:7
 

四、模式语法

#!/usr/bin/env python3
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
    then to stdout
    """
    # Look for each pattern in the text and print the results
    for pattern, desc in patterns:
        print("Pattern {0!r} ({1})\n".format(pattern, desc))
        print("  {0!r}".format(text))
        for match in re.finditer(pattern, text):
            s = match.start()
            e = match.end()
            substr = text[s:e]
            n_backslashes = text[:s].count('\\')
            prefix = '.' * (s + n_backslashes)
            print("  {0}{1!r}".format(prefix, substr))
        print()
    return

if __name__ == "__main__":
    test_patterns('abbaaabbbbaaaaa',
                  [('ab', "'a' followd by 'b'"),])

    test_patterns('abbaabba',
        [('ab*',      'a followed by zero or more b'),
         ('ab+',      'a followed by one or more b'),
         ('ab?',      'a followed by zero or one b'),
         ('ab(3)',    'a followed by three b'),
         ('ab(2, 3)', 'a folloed by two to three b')])

Pattern 'ab' ('a' followd by 'b')

  'abbaaabbbbaaaaa'
  'ab'
  .....'ab'

Pattern 'ab*' (a followed by zero or more b)

  'abbaabba'
  'abb'
  ...'a'
  ....'abb'
  .......'a'

Pattern 'ab+' (a followed by one or more b)

  'abbaabba'
  'abb'
  ....'abb'

Pattern 'ab?' (a followed by zero or one b)

  'abbaabba'
  'ab'
  ...'a'
  ....'ab'
  .......'a'

Pattern 'ab(3)' (a followed by three b)

  'abbaabba'

Pattern 'ab(2, 3)' (a folloed by two to three b)

  'abbaabba'
 

注:如果需要非贪婪模模式,请在模式元字符后面加问好(?)

 

字符集

字符集(character set)是一组字符,包含可以与模式中相应位置匹配的所有字符,例如[ab]可以匹配a或b

test_patterns(
        'abbaabbba',
        [
            ("[ab]",    "either a or b"),
            ("a[ab]+",  "a followed by 1 or more a or b"),
            ("a[ab]+?", "a followed by 1 or more a or b, not greedy"),
        ]
    )

Pattern '[ab]' (either a or b)

  'abbaabbba'
  'a'
  .'b'
  ..'b'
  ...'a'
  ....'a'
  .....'b'
  ......'b'
  .......'b'
  ........'a'

Pattern 'a[ab]+' (a followed by 1 or more a or b)

  'abbaabbba'
  'abbaabbba'

Pattern 'a[ab]+?' (a followed by 1 or more a or b, not greedy)

  'abbaabbba'
  'ab'
  ...'aa'

Andrew Allcot 说:
2019年6月03日 00:24

The library is the place where you can get the whole collection of the books of all type with the formation f many collections. The Chinese library example of the https://aussiessayservices.com/essayroo-com-review/ article is presented in the formation of ways by making the kind things.

seo service UK 说:
2024年1月15日 21:57

nice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this

seo service UK 说:
2024年2月23日 20:35

It is a completely interesting blog publish.I often visit your posts for my project's help about Diwali Bumper Lottery and your super writing capabilities genuinely go away me taken aback.Thank you a lot for this publish.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter