Skip to content

Commit

Permalink
feat(Parser): Set regex methods new return types as TextHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
D4Vinci committed Jan 30, 2025
1 parent 93be416 commit e507778
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions scrapling/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ def json(self) -> Dict:
return self.get_all_text(strip=True).json()

def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True,
clean_match: bool = False, case_sensitive: bool = False) -> 'List[str]':
clean_match: bool = False, case_sensitive: bool = False) -> TextHandlers:
"""Apply the given regex to the current text and return a list of strings with the matches.
:param regex: Can be either a compiled regular expression or a string.
Expand All @@ -799,7 +799,7 @@ def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True,
return self.text.re(regex, replace_entities, clean_match, case_sensitive)

def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True,
clean_match: bool = False, case_sensitive: bool = False) -> Union[str, None]:
clean_match: bool = False, case_sensitive: bool = False) -> TextHandler:
"""Apply the given regex to text and return the first match if found, otherwise return the default value.
:param regex: Can be either a compiled regular expression or a string.
Expand Down Expand Up @@ -1048,7 +1048,7 @@ def css(self, selector: str, identifier: str = '', auto_save: bool = False, perc
return self.__class__(flatten(results))

def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True,
clean_match: bool = False, case_sensitive: bool = False) -> 'List[str]':
clean_match: bool = False, case_sensitive: bool = False) -> TextHandlers[TextHandler]:
"""Call the ``.re()`` method for each element in this list and return
their results flattened as List of TextHandler.
Expand All @@ -1060,10 +1060,10 @@ def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True,
results = [
n.text.re(regex, replace_entities, clean_match, case_sensitive) for n in self
]
return flatten(results)
return TextHandlers(flatten(results))

def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True,
clean_match: bool = False, case_sensitive: bool = False) -> Union[str, None]:
clean_match: bool = False, case_sensitive: bool = False) -> TextHandler:
"""Call the ``.re_first()`` method for each element in this list and return
the first result or the default value otherwise.
Expand Down

0 comments on commit e507778

Please # to comment.