-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyRegex.py
More file actions
168 lines (132 loc) · 5.14 KB
/
PyRegex.py
File metadata and controls
168 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
A simple implementation of the python.re package.
Copyright (c) 2017-2022 Funilrys - Nissar Chababy <contact at funilrys dot com>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original Version: https://github.com/funilrys/PyRegex
""""
from re import compile as comp
from re import sub as substrings
from re import escape
# pylint: disable=invalid-name
class Regex(object): # pylint: disable=too-few-public-methods
"""
A simple implementation ot the python.re package
Arguments:
- data: str or list
The data or a list of data to check.
- regex: str or list
The regex or a list or regex.
- return_data: bool
- True: Return matched string
- False: Return False|True
- group: int
The group to return.
- rematch: bool
Implementation of Bash ${BASH_REMATCH}.
- True: Returned matched groups into a list format.
- replace_with: str
The value to replace the matched regex with.
- occurences: int
The number of occurence to replace.
"""
def __init__(self, data, regex, **args):
super(Regex, self).__init__()
# We initiate the needed variable in order to be usable all over class
self.data = data
self.regex = regex
# We assign the default value of our optional arguments
optional_arguments = {
"escape": False,
"group": 0,
"occurences": 0,
"rematch": False,
"replace_with": None,
"return_data": True,
}
# We initiate our optional_arguments in order to be usable all over the
# class
for (arg, default) in optional_arguments.items():
setattr(self, arg, args.get(arg, default))
# We initiate regex according to self.escape status.
if self.escape: # pylint: disable=no-member
self.regex = escape(regex)
else:
self.regex = regex
def match(self, regex=None, data_to_match=None):
"""
Used to get exploitable result of re.search
Arguments:
- data: str
The data or a list of data to check.
- regex: str
The regex or a list or regex.
Returns:
list or bool
- bool: if self.return_data is False
- list: otherwise
"""
# We initate this variable which gonna contain the returned data
result = []
if not regex:
regex = self.regex
if not data_to_match:
data_to_match = self.data
# We compile the regex string
to_match = comp(regex)
# In case we have to use the implementation of ${BASH_REMATCH} we use
# re.findall otherwise, we use re.search
if self.rematch: # pylint: disable=no-member
pre_result = to_match.findall(data_to_match)
else:
pre_result = to_match.search(data_to_match)
if self.return_data and pre_result is not None: # pylint: disable=no-member
if self.rematch: # pylint: disable=no-member
for data in pre_result:
if isinstance(data, tuple):
result.extend(list(data))
else:
result.append(data)
if self.group != 0: # pylint: disable=no-member
return result[self.group] # pylint: disable=no-member
else:
result = pre_result.group(
self.group # pylint: disable=no-member
).strip()
return result
elif not self.return_data and pre_result is not None: # pylint: disable=no-member
return True
return False
def not_matching_list(self):
"""
This method return a list of string which don't match the
given regex.
"""
pre_result = comp(self.regex)
return list(
filter(lambda element: not pre_result.search(str(element)), self.data)
)
def matching_list(self):
"""
This method return a list of the string which match the given
regex.
"""
pre_result = comp(self.regex)
return list(filter(lambda element: pre_result.search(str(element)), self.data))
def replace(self):
"""
Used to replace a matched string with another.
"""
if self.replace_with is not None: # pylint: disable=no-member
return substrings(
self.regex,
self.replace_with, # pylint: disable=no-member
self.data,
self.occurences, # pylint: disable=no-member
)
return self.data