Coverage for wrapclib/re2/re2.py: 100%
30 statements
« prev ^ index » next coverage.py v6.4.2, created at 2023-02-02 00:32 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2023-02-02 00:32 +0100
1"""
2Copyright (c) 2010, David Reiss and Facebook, Inc. All rights reserved.
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions
6are met:
7* Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9* Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12* Neither the name of Facebook nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27"""
29import sre_constants
30from ._re2 import _compile # pylint: disable=E0611,E0401
31from ._re2 import escape, Set, UNANCHORED, ANCHOR_START, ANCHOR_BOTH # pylint: disable=E0611,E0401
33__all__ = [
34 "error",
35 "escape",
36 "compile",
37 "search",
38 "match",
39 "findall",
40 "fullmatch",
41 "Set",
42 "UNANCHORED",
43 "ANCHOR_START",
44 "ANCHOR_BOTH",
45]
47error = sre_constants.error # pylint: disable=E1101
50def compile(pattern): # pylint: disable=W0622
51 "Compiles a regular expression pattern, returning a pattern object."
52 return _compile(pattern, error)
55def search(pattern, string, pos=0, endpos=-1):
56 """Scans through string looking for a match to the pattern, returning
57 a match object, or None if no match was found."""
58 if endpos != -1:
59 return _compile(pattern, error).search(string, pos, endpos)
60 else:
61 return _compile(pattern, error).search(string, pos)
64def match(pattern, string):
65 """Tries to apply the pattern at the start of the string, returning
66 a match object, or None if no match was found."""
67 return _compile(pattern, error).match(string)
70def fullmatch(pattern, string):
71 """Tries to apply the pattern to the entire string, returning
72 a match object, or None if no match was found."""
73 return _compile(pattern, error).fullmatch(string)
76def findall(pattern, string, pos=0, endpos=-1):
77 """
78 Implements method :epkg:`*py:re:findall`
79 for *re2* in :epkg:`Python`.
81 @param pattern compiled regular expression
82 @param string string to search
83 @param pos first position to look into
84 @param endpos last position to look into (-1 for the last one)
85 @return list of results
87 .. exref::
88 :title: Example of findall
90 A quick example with method
91 @see fn findall.
93 .. runpython::
94 :showcode:
96 from wrapclib import re2
98 s = "date 0 : 14/9/2000 date 1 : 20/04/1971 "
100 reg = re2.compile(
101 "([0-3]?[0-9]/[0-1]?[0-9]/([0-2][0-9])?[0-9][0-9])[^\\d]")
103 fall = re2.findall(reg, s)
104 print(fall)
105 """
106 if isinstance(pattern, str):
107 pattern = _compile(pattern, error)
108 results = []
109 if endpos == -1:
110 def fsearch(s, p):
111 return pattern.search(s, p)
112 else:
113 def fsearch(s, p):
114 return pattern.search(s, p, endpos)
115 res = fsearch(string, pos)
116 while res:
117 results.append(res.groups())
118 pos = res.span()[1]
119 res = fsearch(string, pos)
120 return results