scientific_string/__init__.py
def match_one_string(pattern: str, s: str, *args):
    """
    Make sure you know only none or one string will be matched! If you are not sure, use `match_one_pattern` instead.

    :param pattern:
    :param s:
    :param args:
    :return:

    .. doctest::

        >>> p = "\d+"
        >>> s = "abc 123 def"
        >>> match_one_string(p, s, int)
        123
        >>> print(match_one_string(p, "abc"))
        Pattern "\d+" not found, or more than one found in string abc!
        None
        >>> print(match_one_string(p, "abc 123 def 456"))
        Pattern "\d+" not found, or more than one found in string abc 123 def 456!
        None
    """
    try:
        # `match` is either an empty list or a list of string.
        match, = re.findall(pattern, s)
        if len(args) == 0:  # If no wrapper argument is given, return directly the matched string
            return match
        elif len(args) == 1:  # If wrapper argument is given, i.e., not empty, then apply wrapper to the match
            wrapper, = args
            return wrapper(match)
        else:
            raise TypeError(
                'Multiple wrappers are given! Only one should be given!')
    except ValueError:
        print("Pattern \"{0}\" not found, or more than one found in string {1}!".format(
            pattern, s))

    Make sure you know only none or one string will be matched! If you are not sure, use `match_one_pattern` instead.

    :param pattern:
    :param s:
    :param args:
    :return:

    .. doctest::

        >>> p = "\d+"
        >>> s = "abc 123 def"
        >>> match_one_string(p, s, int)
        123
        >>> print(match_one_string(p, "abc"))
        Pattern "\d+" not found, or more than one found in string abc!
        None
        >>> print(match_one_string(p, "abc 123 def 456"))
        Pattern "\d+" not found, or more than one found in string abc 123 def 456!
        None
    
Make sure you know only none or one string will be matched. If you are not sure, use `match_one_pattern` instead.
Make sure you know only none or one string will be matched . If you are not sure use ` match_one_pattern ` instead .
def match_one_string(pattern: str, s: str, *args):
    try:
        match, = re.findall(pattern, s)
        if len(args) == 0:  
            return match
        elif len(args) == 1:  
            wrapper, = args
            return wrapper(match)
        else:
            raise TypeError(
                'Multiple wrappers are given! Only one should be given!')
    except ValueError:
        print("Pattern \"{0}\" not found, or more than one found in string {1}!".format(
            pattern, s))
def match_one_string ( pattern : str , s : str , * args ) : try : match , = re . findall ( pattern , s ) if len ( args ) == 0 : return match elif len ( args ) == 1 : wrapper , = args return wrapper ( match ) else : raise TypeError ( 'Multiple wrappers are given! Only one should be given!' ) except ValueError : print ( "Pattern \"{0}\" not found, or more than one found in string {1}!" . format ( pattern , s ) )
Make sure you know only none or one string will be matched!
Make sure you know only none or one string will be matched!
"""
    Make sure you know only none or one string will be matched! If you are not sure, use `match_one_pattern` instead.

    :param pattern:
    :param s:
    :param args:
    :return:

    .. doctest::

        >>> p = "\d+"
        >>> s = "abc 123 def"
        >>> match_one_string(p, s, int)
        123
        >>> print(match_one_string(p, "abc"))
        Pattern "\d+" not found, or more than one found in string abc!
        None
        >>> print(match_one_string(p, "abc 123 def 456"))
        Pattern "\d+" not found, or more than one found in string abc 123 def 456!
        None
    """ # `match` is either an empty list or a list of string. # If no wrapper argument is given, return directly the matched string # If wrapper argument is given, i.e., not empty, then apply wrapper to the match
:
>>> p = "\d+"
>>> s = "abc 123 def"
>>> match_one_string(p, s, int)
123
>>> print(match_one_string(p, "abc"))
Pattern "\d+" not found, or more than one found in string abc.
None
>>> print(match_one_string(p, "abc 123 def 456"))
Pattern "\d+" not found, or more than one found in string abc 123 def 456.
None
: >>> p = " \ d + " >>> s = " abc 123 def " >>> match_one_string ( p s int ) 123 >>> print ( match_one_string ( p " abc " )) Pattern " \ d + " not found or more than one found in string abc . None >>> print ( match_one_string ( p " abc 123 def 456 " )) Pattern " \ d + " not found or more than one found in string abc 123 def 456 . None
pattern
str
None
s
str
None
args
None