rossant/ipymd · prompt.py
python logo
def _starts_with_regex(line, regex):
    """Return whether a line starts with a regex or not."""
    if not regex.startswith('^'):
        regex = '^' + regex
    reg = re.compile(regex)
    return reg.match(line)
Similar code snippets
1.
knipknap/exscript · cast.py
Match rating: 69.34% · See similar code snippets
python logo
def to_regex(regex, flags=0):
    """
    Given a string, this function returns a new re.RegexObject.
    Given a re.RegexObject, this function just returns the same object.

    :type  regex: string|re.RegexObject
    :param regex: A regex or a re.RegexObject
    :type  flags: int
    :param flags: See Python's re.compile().
    :rtype:  re.RegexObject
    :return: The Python regex object.
    """
    if regex is None:
        raise TypeError('None can not be cast to re.RegexObject')
    if hasattr(regex, 'match'):
        return regex
    return re.compile(regex, flags)
2.
usc-isi-i2/dig-regex-extractor · regex_extractor.py
Match rating: 68.23% · See similar code snippets
python logo
def set_regex(self, regex):
        if not (isinstance(regex, type(re.compile(''))) or
                isinstance(regex, types.ListType)):
            raise ValueError("regex must be a Regex or a list of Regexes")
        self.regex = regex
        return self
3.
refnode/liquid · strscan.py
Match rating: 67.45% · See similar code snippets
python logo
def get_regex(regex):
    """
    Ensure we have a compiled regular expression object.

        >>> import re
        >>> get_regex('string') # doctest: +ELLIPSIS
        <_sre.SRE_Pattern object at 0x...>
        >>> pattern = re.compile(r'string')
        >>> get_regex(pattern) is pattern
        True
        >>> get_regex(3) # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        TypeError: Invalid regex type: 3
    """
    if isinstance(regex, basestring):
        return re.compile(regex)
    elif not isinstance(regex, re._pattern_type):
        raise TypeError("Invalid regex type: %r" % (regex,))
    return regex
4.
DataDog/integrations-core · regex.py
Match rating: 66.83% · See similar code snippets
python logo
def from_native(cls, regex):
        """Convert a Python regular expression into a ``Regex`` instance.

        Note that in Python 3, a regular expression compiled from a
        :class:`str` has the ``re.UNICODE`` flag set. If it is undesirable
        to store this flag in a BSON regular expression, unset it first::

          >>> pattern = re.compile('.*')
          >>> regex = Regex.from_native(pattern)
          >>> regex.flags ^= re.UNICODE
          >>> db.collection.insert({'pattern': regex})

        :Parameters:
          - `regex`: A regular expression object from ``re.compile()``.

        .. warning::
           Python regular expressions use a different syntax and different
           set of flags than MongoDB, which uses `PCRE`_. A regular
           expression retrieved from the server may not compile in
           Python, or may match a different set of strings in Python than
           when used in a MongoDB query.

        .. _PCRE: http://www.pcre.org/
        """
        if not isinstance(regex, RE_TYPE):
            raise TypeError(
                "regex must be a compiled regular expression, not %s"
                % type(regex))

        return Regex(regex.pattern, regex.flags)
5.
cos-archives/modular-odm · __init__.py
Match rating: 66.32% · See similar code snippets
python logo
def __init__(self, regex=None, flags=0):

        if regex is not None:
            self.regex = re.compile(regex, flags=flags)
6.
shanbay/peeweext · validation.py
Match rating: 65.59% · See similar code snippets
python logo
def __init__(self, regex):
        self._regex = regex
        self._compiled_regex = re.compile(regex)
7.
Captricity/captools · client.py
Match rating: 65.54% · See similar code snippets
python logo
def _split_regex(regex):
    """
    Return an array of the URL split at each regex match like (?P<id>[\d]+)
    Call with a regex of '^/foo/(?P<id>[\d]+)/bar/$' and you will receive ['/foo/', '/bar/']
    """
    if regex[0] == '^':
        regex = regex[1:]
    if regex[-1] == '$':
        regex = regex[0:-1]
    results = []
    line = ''
    for c in regex:
        if c == '(':
            results.append(line)
            line = ''
        elif c == ')':
            line = ''
        else:
            line = line + c
    if len(line) > 0:
        results.append(line)
    return results
8.
petermelias/valhalla · strings.py
Match rating: 65.49% · See similar code snippets
python logo
def regex(regex, case=False, _value=None, *args, **kwargs):
    if kwargs.get('case'):
        regex = re.compile(regex)
    else:
        regex = re.compile(regex, re.IGNORECASE)

    if not regex.match(_value):
        raise ValidationError('The _value must match the regex %s' % regex)

    return _value
9.
maximkulkin/hypothesis-regex · hypothesis_regex.py
Match rating: 65.41% · See similar code snippets
python logo
def regex(regex):
    """Return strategy that generates strings that match given regex.

    Regex can be either a string or compiled regex (through `re.compile()`).

    You can use regex flags (such as `re.IGNORECASE`, `re.DOTALL` or `re.UNICODE`)
    to control generation. Flags can be passed either in compiled regex (specify
    flags in call to `re.compile()`) or inside pattern with (?iLmsux) group.

    Some tricky regular expressions are partly supported or not supported at all.
    "^" and "$" do not affect generation. Positive lookahead/lookbehind groups
    are considered normal groups. Negative lookahead/lookbehind groups do not do
    anything. Ternary regex groups ('(?(name)yes-pattern|no-pattern)') are not
    supported at all.
    """
    if not hasattr(regex, 'pattern'):
        regex = re.compile(regex)

    pattern = regex.pattern
    flags = regex.flags

    codes = sre.parse(pattern)

    return _strategy(codes, Context(flags=flags)).filter(regex.match)
10.
NiklasRosenstein-Python/nr-deprecated · strex.py
Match rating: 65.31% · See similar code snippets
python logo
def __init__(self, name, regex, flags=0, skip=False):
    super(Regex, self).__init__(name, skip)
    if isinstance(regex, string_types):
      regex = re.compile(regex, flags)
    self.regex = regex