@@ -63,6 +63,7 @@ class RegexHelper:
6363 """
6464
6565 _regex : Optional [str ] = None
66+ _compiled : Optional [re .Pattern ] = None
6667 escape_regex : bool = False
6768
6869 def __init__ (self , regex : Optional [str ] = None , escape_regex : bool = False ):
@@ -99,6 +100,8 @@ def regex(self, value: str) -> None:
99100 else :
100101 self ._regex = re .escape (value )
101102
103+ self ._compiled = re .compile (self ._regex )
104+
102105 def set_regex (self , value : str ) -> "RegexHelper" :
103106 """
104107 Sets the regex to work with.
@@ -120,19 +123,15 @@ def get_not_matching_list(self, data: List[str]) -> List[str]:
120123 in the given data.
121124 """
122125
123- pre_result = re .compile (self .regex )
124-
125- return [x for x in data if not pre_result .search (str (x ))]
126+ return [x for x in data if not self ._compiled .search (str (x ))]
126127
127128 def get_matching_list (self , data : List [str ]) -> List [str ]:
128129 """
129130 Returns the strings which does the match the regex
130131 in the given data.
131132 """
132133
133- pre_result = re .compile (self .regex )
134-
135- return [x for x in data if pre_result .search (str (x ))]
134+ return [x for x in data if self ._compiled .search (str (x ))]
136135
137136 def match (
138137 self ,
@@ -157,12 +156,11 @@ def match(
157156 """
158157
159158 result = []
160- to_match = re .compile (self .regex )
161159
162160 if rematch :
163- pre_result = to_match .findall (data )
161+ pre_result = self . _compiled .findall (data )
164162 else :
165- pre_result = to_match .search (data )
163+ pre_result = self . _compiled .search (data )
166164
167165 if return_match and pre_result :
168166 if rematch :
@@ -209,7 +207,7 @@ def replace_match(
209207 self .regex ,
210208 replacement ,
211209 data ,
212- occurences ,
210+ count = occurences ,
213211 flags = re .MULTILINE if multiline else 0 ,
214212 )
215213 return data
@@ -222,4 +220,4 @@ def split(self, data: str) -> List[str]:
222220 :rtype: list
223221 """
224222
225- return re . split (self . regex , data )
223+ return self . _compiled . split (data )
0 commit comments