Source code for PyFunceble.helpers.regex

The tool to check the availability or syntax of domain, IP or URL.


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the regular expressions helpers.

    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:


Project link:

Project documentation:

Project homepage:


    Copyright 2017, 2018, 2019, 2020, 2021 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
from re import MULTILINE
from re import compile as re_compile
from re import escape as re_escape
from re import split as re_split
from re import sub as re_sub

[docs]class Regex: """ Simplify the regex matching and usage. :param str regex: The regex to use. :param bool escape: Escapes the given regex. """ def __init__(self, regex, escape=False): if escape: self.regex = re_escape(regex) else: self.regex = regex
[docs] def get_not_matching_list(self, data): """ Returns the strings which does not the match the regex in the given data. """ pre_result = re_compile(self.regex) return [x for x in data if not]
[docs] def get_matching_list(self, data): """ Returns the strings which does the match the regex in the given data. """ pre_result = re_compile(self.regex) return [x for x in data if]
[docs] def match(self, data, rematch=False, group=0, return_match=True): """ Checks if the given data match the given regex string. :param str data: The data to work with. :param bool rematch: The equivalent of the $BASH_REMATCH but in Python. It's basically a list of all groups. :param bool group: The group to return when return_match is set to :code:`True`. :param bool return_match: Return the part that match the given regex string. """ result = [] to_match = re_compile(self.regex) if rematch: pre_result = to_match.findall(data) else: pre_result = if return_match and pre_result: if rematch: for res in pre_result: if isinstance(res, tuple): result.extend(list(res)) else: result.append(res) if group != 0: return result[group] else: result = return result if not return_match and pre_result: return True return False
[docs] def replace_match(self, data, replacement, occurences=0, multiline=False): """ Replaces the string which match the regex string with the given replacement. :param str data: The data to work with. :param str replacement: The replacement of the matched regex. :param int occurences: The number of occurences to replace. .. note:: :code:`0` means all occurences. :rtype: str """ if isinstance(replacement, str): return re_sub( self.regex, replacement, data, occurences, flags=MULTILINE if multiline else 0, ) return data
[docs] def split(self, data): """ Split the reference of the given regex. :param str data: The data to work with. :rtype: list """ return re_split(self.regex, data)