Exemple #1
0
def main():
    test_patterns(
        "abbaabbba",
        [("a.", "a followed by any one character"),
         ("b.", "b followed by any one character"),
         ("a.*b", "a followed by anything, ending in b"),
         ("a.*?b", "a followed by anything, ending in b, non-greedy")])
def main():
    test_patterns("A prime #1 example!", [
        (r"\d+", "sequence of digits"),
        (r"\D+", "sequence of non-digits"),
        (r"\s+", "sequence of whitespace"),
        (r"\S+", "sequence of non-whitespace"),
        (r"\w+", "alphanumeric characters"),
        (r"\W+", "non-alphanumeric"),
    ])
Exemple #3
0
def main():
    test_patterns(
        "abbaaabbbbaaaaa",
        [
            (r"a(ab)", "a followed by literal ab"),
            (r"a(a*b*)", "a followed by 0-n a and 0-n b"),
            (r"a(ab)*", "a followed by 0-n ab"),
            ("a(ab)+", "a followed by 1-n ab")
        ]
    )
def main():
    test_patterns("This is some text -- with punctuation.",
                  [(r"^\w+", "word at start of string"),
                   (r"\A\w+", "word at start of string"),
                   (r"\w+\S*$", "word near end of string"),
                   (r"\w+\S*\Z", "word near end of string"),
                   (r"\w*t\w*", "word containing t"),
                   (r"\bt\w+", "t at start of word"),
                   (r"\w+t\b", "t at end of word"),
                   (r"\Bt\B", "t not at start or end of word")])
#!/usr/bin/python

from re_test_patterns import test_patterns

test_patterns(
    'A prime #1 example!',
    [(r'\d+', 'sequence of digits'),
    (r'\D+', 'sequence of nodigits'),
    (r'\s+', 'sequence of whitespace'),
    (r'\S+', 'sequence of nonwhitespace'),
    (r'\w+', 'alphanumeric characters'),
    (r'\W+', 'nonalphanumeric')
    ]
        )

Exemple #6
0
def main():
    test_patterns("abbaabbba", [("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 followed by two to three b")])
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
# end_pymotw_header

from re_test_patterns import test_patterns

test_patterns("This is some text -- with punctuation.", ["[^-. ]+"])  # sequences without -, ., or space
from re_test_patterns import test_patterns

test_patterns(
    'abbaabbba',
    [('a.', 'a followed by any one character'),
     ('b.', 'b followed by any one character'),
     ('a.*b', 'a followed by anything, ending in b'),
     ('a.*?b', 'a followed by anything, ending in b')],
)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Repetition of patterns
"""

from re_test_patterns import test_patterns

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'),
    ]
)
# re_groups.py

from re_test_patterns import test_patterns

test_patterns(
    'abbaaabbbbaaaaa',
    [('a(ab)', 'a seguito da ab letterale'),
     ('a(a*b*)', 'a seguito da 0-n a e 0-n b'),
     ('a(ab)*', 'a seguito da 0-n ab'),
     ('a(ab)+', 'a seguito da 1-n ab')],
)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              [ 'a(ab)',    # 'a' seguito da 'ab' letterale
                'a(a*b*)',  # 'a' seguito da 0-n 'a' e 0-n 'b'
                'a(ab)*',   # 'a' seguito da 0-n 'ab'
                'a(ab)+',   # 'a' seguito da 1-n 'ab'
                ])
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from re_test_patterns import test_patterns

test_patterns(
    'abbaabbba',
    [('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 followed by two to three b')],
)

#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [ ('[^-. ]+',  'sequences without -, ., or space'),
      ])
Exemple #14
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escape codes
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns('This is a prime #1 example!',
              [ r'\d+', # sequence of digits
                r'\D+', # sequence of non-digits
                r'\s+', # sequence of whitespace
                r'\S+', # sequence of non-whitespace
                r'\w+', # alphanumeric characters
                r'\W+', # non-alphanumeric
                ])
# re_repetition_non_greedy.py

from re_test_patterns import test_patterns

test_patterns(
    'abbaabbba',
    [('ab*?', 'a seguito da zero o più b'),
     ('ab+?', 'a seguito da one o più b'),
     ('ab??', 'a seguito da zero od una b'),
     ('ab{3}?', 'a seguito da tre b'),
     ('ab{2,3}?', 'a seguito da due fino a tre b')],
)
# re_anchoring.py

from re_test_patterns import test_patterns

test_patterns(
    'Trova in parte di testo -- con punteggiatura.',
    [(r'^\w+', 'parola ad inizio stringa'),
     (r'\A\w+', 'parola ad inizio stringa'),
     (r'\w+\S*$', 'parola verso la fine della stringa, senza punteggiatura'),
     (r'\w+\S*\Z', 'parola verso la fine della stringa, senza punteggiatura'),
     (r'\w*t\w*', 'parola che contiene t'),
     (r'\bt\w+', 't ad inizio della parola'),
     (r'\w+t\b', 't alla fine della parola'),
     (r'\Bt\B', 't, non all\'inizio o fine della parola')],
)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('Questa porzione di testo -- con punteggiatura.',
              [ '[a-z]+',      # sequenza di lettere minuscole
                '[A-Z]+',      # sequenza di lettere maiuscole
                '[a-zA-Z]+',   # sequenza di lettere maiuscole o minuscole
                '[A-Z][a-z]+', # una lettera maiuscola seguita da lettere  minuscole
                ])
Exemple #18
0
# -*- coding: utf-8 -*-

"""贪婪匹配"""
from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              ['ab*',
               'ab+',
               'ab?',
               'ab{3}',
               'ab{2,3}'])
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""

# end_pymotw_header
from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [('[^-. ]+', 'sequences without -, ., or space')],
)
test_patterns(
    'This + is /some  text --() with punctuation.',
    [('[^-./+() ]+', 'sequences without -, ., or space')],
)
test_patterns(
    'This + is /some  text --() with punctuation.',
    [('[-./+() ]+', 'sequences without -, ., or space')],
)
from re_test_patterns import test_patterns

test_patterns('abbaabbba', [('ab*?', 'a follow by zero or more b'),
                            ('ab+?', 'a follow by one or more b'),
                            ('ab??', 'a follow by zero or more b'),
                            ('ab{3}?', 'a follow by three b'),
                            ('ab{2,3}?', 'a follow by two to three b')])
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('This is a prime #1 example!',
              [ r'\d+', # sequenza di cifre
                r'\D+', # sequenza di non-cifre
                r'\s+', # sequenza di whitespace
                r'\S+', # sequenza di non-whitespace
                r'\w+', # caratteri alfanumerici
                r'\W+', # non-alfanumerici
                ])
Exemple #22
0
from re_test_patterns import test_patterns

test_patterns(
    'abbaaabbbbaaaaa',
    [('a(ab)', 'a followed by literal ab'),
     ('a(a*b*)', 'a followed by 0-n a and 0-n b'),
     ('a(ab)*', 'a followed by 0-n ab'), ('a(ab)+', 'a followed by 1-n ab')],
)
Exemple #23
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns('This is some text -- with punctuation.',
              [ '[a-z]+',      # sequences of lower case letters
                '[A-Z]+',      # sequences of upper case letters
                '[a-zA-Z]+',   # sequences of lower or upper case letters
                '[A-Z][a-z]+', # one upper case letter followed by lower case letters
                ])
Exemple #24
0
from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [('[a-z]+', 'sequences of lowercase letters'),
     ('[A-Z]+', 'sequences of uppercase letters'),
     ('[a-zA-Z]+', 'sequences of lower- or uppercase letters'),
     ('[A-Z][a-z]+', 'one uppercase followed by lowercase')],
)
Exemple #25
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [
        '[^-. ]+',  # sequences without -, ., or space
    ])
Exemple #26
0
#!/usr/bin/env python
# -*- coding:UTF-8 -*-

from re_test_patterns import test_patterns

# 禁用贪心模式
test_patterns('abbaabbba', [
    ('ab*?', '>=0'),
    ('ab+?', '>=1'),
    ('ab??', '0 <= num <=1'),
    ('ab{3}?', 'three times'),
    ('ab{2,3}?', 'two to three'),
])
Exemple #27
0
# -*- coding: utf-8 -*-

"""
A character set is a group of characters, any one of which can match at that
point in the pattern. For example, [ab]
would match either a or b.

The greedy form of the expression, a[ab]+, consumes the entire string because
the first letter is a and every subsequent
character is either a or b.
"""

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              ['ab',            # either a or b
               'a[ab]+',        # a followed by one more a or b
               'a[ab]+?'])      # a followed by one or more a or b, not greedy
from re_test_patterns import test_patterns

test_patterns('This is some text -- with punctuation',
              [('[^-. ]+', 'sequences without -,., or  space')])
Exemple #29
0
# -*- coding: utf-8 -*-

from re_test_patterns import test_patterns

test_patterns('abbaabbba', [
    ('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 followed by two to three b'),
])
Exemple #30
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Anchoring the search
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns('This is some text -- with punctuation.',
              [ r'^\w+',     # word at start of string
                r'\A\w+',    # word at start of string
                r'\w+\S*$',  # word at end of string, with optional punctuation
                r'\w+\S*\Z', # word at end of string, with optional punctuation
                r'\w*t\w*',  # word containing 't'
                r'\bt\w+',   # 't' at start of word
                r'\w+t\b',   # 't' at end of word
                r'\Bt\B',    # 't', not start or end of word
                ])
Exemple #31
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Regular expression grouping
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              [ 'a(ab)',    # 'a' followed by literal 'ab'
                'a(a*b*)',  # 'a' followed by 0-n 'a' and 0-n 'b'
                'a(ab)*',   # 'a' followed by 0-n 'ab'
                'a(ab)+',   # 'a' followed by 1-n 'ab'
                ])
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              [ 'a.',   # a seguito da qualsiasi carattere
                'b.',   # b seguito da qualsiasi carattere
                'a.*b', # a seguito da qualunque cosa, che finisca per b
                'a.*?b', # a seguito da qualunque cosa, che finisca per b
                ])
Exemple #33
0
from re_test_patterns import test_patterns

test_patterns(
    r'\d+ \D+ \s',
    [(r'\\.\+', 'escape code')],
)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns(
    "abbaaabbbbaaaaa",
    [
        "ab*",  # a seguito da zero o più b
        "ab+",  # a seguito da uno o più b
        "ab?",  # a seguito da zero od una b
        "ab{3}",  # a seguito da tre b
        "ab{2,3}",  # a seguito da due a tre b
    ],
)
Exemple #35
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from re_test_patterns import test_patterns

test_patterns(
    'abbaabbba',
    [('a.', 'a followed by any one character'),
     ('b.', 'b followed by any one character'),
     ('a.*b', 'a followed by anything, ending in b'),
     ('a.*?b', 'a followed by anything, ending in b')],
)


# -*- coding: utf-8 -*-	
#1.3.4模式语法
print '1.3.4模式语法\n'
from re_test_patterns import test_patterns
#函数见re_test_patterns.py
test_patterns(
	'abbaaabbbbaaaaa',
	[('ab', "'a' followed by 'b'"),
	])
test_patterns(
	'abbaabbba',
	[ ('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 followed by two to three b'), 
	])
#在重复指令后面加上"?"可以关闭贪心行为
print '在重复指令后面加上"?"可以关闭贪心行为\n'
test_patterns(
	'abbaabbba',
	[ ('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 followed by two to three b'), 
	])	
#字符集
print '字符集\n'
#字符集是一组字符,包含可以与模式中相应位置匹配的所有字符。例如[ab]可以匹配a或b
test_patterns(
Exemple #37
0
from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [('[^-. ]+', 'sequences without -, ., or space')],
)

# re_charset_ranges
test_patterns(
    'This is some text -- with punctuation.',
    [('[a-z]+', 'sequences of lowercase letters'),
     ('[A-Z]+', 'sequences of uppercase letters'),
     ('[a-zA-Z]+', 'sequences of letters of either case'),
     ('[A-Z][a-z]+', 'one uppercase followed by lowercase')],
)


#re_charset_dot.py
from re_test_patterns import test_patterns

test_patterns(
    'abbaabbba',
    [('a.', 'a followed by any one character'),
     ('b.', 'b followed by any one character'),
     ('a.*b', 'a followed by anything, ending in b'),
     ('a.*?b', 'a followed by anything, ending in b')],
)

# re_escape_codes.py
from re_test_patterns import test_patterns
Exemple #38
0
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escaping escape codes
"""

#end_pymotw_header
from re_test_patterns import test_patterns

test_patterns(
    r'\d+ \D+ \s+',
    # escapes the backslash and plus characters, since both are meta-characters and have special meaning in a regular expression.
    # notice raw string here
    [(r'\\.\+', 'escape code')],
)
Exemple #39
0
# -*- coding: utf-8 -*-

"""
As a special case of a character set the metacharacter dot, or period (.),
indicates that the pattern should match any
single character in that position.

Combining dot with repetition can result in very long matches, unless the
non-greedy form is used.
"""

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              ['a.',        # a followed by any one character
               'b.',        # b followed by any one character
               'a.*b',      # a followed by anything, ending in b
               'a.*?b'])    # a followed by anything, ending in b
Exemple #40
0
def main():
    test_patterns("This is some text -- with punctuation.",
                  [(r"[a-z]+", "sequences of lowercase letters"),
                   (r"[A-Z]+", "sequences of uppercase letters"),
                   (r"[a-zA-Z]+", "sequences of lower- or uppercase letters"),
                   (r"[A-Z][a-z]+", "one uppercase followed by lowercase")])
# re_repetition_non_greedy.py
from re_test_patterns import test_patterns

test_patterns("abbaabbba", [("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 follows by three b"),
                            ("ab{2,3}?", "a followed by two to three b")])
Exemple #42
0
#!/usr/bin/env python
# -*- coding:UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('This is some text -- with puntuation.', [
    (r'^\w+', 'word at start of string'),
    (r'\A\w+', 'word at start of string'),
    (r'\w+\S*$', 'word near end of string,skip puntuation'),
    (r'\w+\S*\Z', 'word near end of string,skip puntuation'),
    (r'\w*t\w*', 'word containing t'),
    (r'\bt\w+', 't at start of word'),
    (r'\w+t\b', 't at end of word'),
    (r'\Bt\B', 't ,not start or end of word'),
])
Exemple #43
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Anchoring the search
"""
# end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    "This is some text -- with punctuation.",
    [
        (r"^\w+", "word at start of string"),
        (r"\A\w+", "word at start of string"),
        (r"\w+\S*$", "word near end of string, skip punctuation"),
        (r"\w+\S*\Z", "word near end of string, skip punctuation"),
        (r"\w*t\w*", "word containing t"),
        (r"\bt\w+", "t at start of word"),
        (r"\w+t\b", "t at end of word"),
        (r"\Bt\B", "t, not start or end of word"),
    ],
)
Exemple #44
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escaping escape codes
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(r'\d+ \D+ \s+ \S+ \w+ \W+',
              [ r'\\d\+',
                r'\\D\+',
                r'\\s\+',
                r'\\S\+',
                r'\\w\+',
                r'\\W\+',
                ])
from re_test_patterns import test_patterns

test_patterns(
    'A prime #1 example!',
    [(r'\d+', 'sequence of digits'), (r'\D+', 'sequence of non-digits'),
     (r'\s+', 'sequence of whitespace'),
     (r'\S+', 'sequence of non-whitespace'),
     (r'\w+', 'alphanumeric characters'), (r'\W+', 'non-alphanumeric')],
)
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    'This is some text -- with punctuation.',
    [ ('[a-z]+', 'sequences of lowercase letters'),
      ('[A-Z]+', 'sequences of uppercase letters'),
      ('[a-zA-Z]+', 'sequences of lowercase or uppercase letters'),
      ('[A-Z][a-z]+', 'one uppercase followed by lowercase'),
      ])
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              [ '[ab]',    # sia a che b
                'a[ab]+',  # a seguito da uno o più a oppure b
                'a[ab]+?', # a seguito da uno o più a oppure b, non greedy
                ])
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escaping escape codes
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(r'\d+ \D+ \s+', [
    (r'\\.\+', 'escape code'),
])
Exemple #49
0
from re_test_patterns import test_patterns
test_patterns(
    'abbaabbba',
    [('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 followed by two to three b'),
     ('ab{2,}',
      'a followed by the value must appear at least m times, with no maximum')
     ],
)
Exemple #50
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escape codes
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    'This is a prime #1 example!',
    [
        r'\d+',  # sequence of digits
        r'\D+',  # sequence of non-digits
        r'\s+',  # sequence of whitespace
        r'\S+',  # sequence of non-whitespace
        r'\w+',  # alphanumeric characters
        r'\W+',  # non-alphanumeric
    ])
Exemple #51
0
# -*- coding: utf-8 -*-

from re_test_patterns import test_patterns

test_patterns('This is a prime #1 example!',
              [r'\d+',
               r'\D+',
               r'\s+',
               r'\S+',
               r'\w+',
               r'\W+'])
#!/usr/bin/python
#-*- coding: UTF-8 -*-
from re_test_patterns import test_patterns

test_patterns(
    'abbaaabbbbaaaaa',
    [
        'a.',  # a followed by any one character
        'b.',  # b followed by any one character
        'a.*b',  # a followed by anything,ending in b
        'a.*?b'  # a followed by anything,ending in b --not greedy
    ])
Exemple #53
0
#!/usr/bin/env python
#
# Repetition
#
from re_test_patterns import test_patterns

test_patterns(
    'abbaaabbbbaaaaa',
    [
        '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 followed by two to three b
    ])
Exemple #54
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Regular expression grouping
"""
#end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    'abbaaabbbbaaaaa',
    [ ('a(ab)',    'a followed by literal ab'),
      ('a(a*b*)',  'a followed by 0-n a and 0-n b'),
      ('a(ab)*',   'a followed by 0-n ab'),
      ('a(ab)+',   'a followed by 1-n ab'),
      ])
# re_escape_escapes.py
from re_test_patterns import test_patterns

test_patterns(r"\d+ \D+ \s+", [(r"\\.\+", "escape code")])
Exemple #56
0
# re_charset_dot.py
from re_test_patterns import test_patterns

test_patterns(
    "abbaabbba",
    [("a.", "a followed by any one character"),
     ("b.", "b followed by any one character"),
     ("a.*b", "a followed by anything, ending in b"),
     ("a.*?b", "a followed by anything, ending in b")]
)
Exemple #57
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Escape codes
"""
# end_pymotw_header

from re_test_patterns import test_patterns

test_patterns(
    "A prime #1 example!",
    [
        (r"\d+", "sequence of digits"),
        (r"\D+", "sequence of nondigits"),
        (r"\s+", "sequence of whitespace"),
        (r"\S+", "sequence of nonwhitespace"),
        (r"\w+", "alphanumeric characters"),
        (r"\W+", "nonalphanumeric"),
    ],
)
# -*- coding: utf-8 -*-

"""
The normal processing for a repetition instruction is to consume as much of the
input as possible while matching the
pattern. This so-called greedy behavior may result in fewer individual matches,
or the matches may include more of
the input text than intended. Greediness can be turned off by following the
repetition instruction with ?.

Disabling greedy consumption of the input for any of the patterns where zero
occurences of b are allowed means the
matched substring does not include any b characters.
"""

from re_test_patterns import test_patterns

test_patterns('abbaaabbbbaaaaa',
              ['ab*?',      # a followed by zero or more b
               'ab+?',      # a followed by one or more b
               'ab??',      # a followed zero or one b
               'ab{3}?',    # a followed by three b
               'ab{2,3}?']) # a followed by two to three b
Exemple #59
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Repetition of patterns
"""
#end_pymotw_header

from re_test_patterns import test_patterns

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'),
])