本文整理匯總了Python中re.VERBOSE屬性的典型用法代碼示例。如果您正苦於以下問題:Python re.VERBOSE屬性的具體用法?Python re.VERBOSE怎麽用?Python re.VERBOSE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類re
的用法示例。
在下文中一共展示了re.VERBOSE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: str2int
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def str2int(string):
numeric_const_pattern = r"""
[-+]? # optional sign
(?:
(?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
|
(?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
)
# followed by optional exponent part if desired
(?: [Ee] [+-]? \d+ ) ?
"""
rx = re.compile(numeric_const_pattern, re.VERBOSE)
nb = rx.findall(string)
for i in enumerate(nb): nb[i[0]] = int(i[1])
return np.array(nb)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
示例2: set_memlimit
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def set_memlimit(limit):
global max_memuse
global real_max_memuse
sizes = {
'k': 1024,
'm': _1M,
'g': _1G,
't': 1024*_1G,
}
m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit,
re.IGNORECASE | re.VERBOSE)
if m is None:
raise ValueError('Invalid memory limit %r' % (limit,))
memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
real_max_memuse = memlimit
if memlimit > MAX_Py_ssize_t:
memlimit = MAX_Py_ssize_t
if memlimit < _2G - 1:
raise ValueError('Memory limit %r too low to be useful' % (limit,))
max_memuse = memlimit
示例3: str_flags_to_int
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def str_flags_to_int(str_flags):
flags = 0
if "i" in str_flags:
flags |= re.IGNORECASE
if "l" in str_flags:
flags |= re.LOCALE
if "m" in str_flags:
flags |= re.MULTILINE
if "s" in str_flags:
flags |= re.DOTALL
if "u" in str_flags:
flags |= re.UNICODE
if "x" in str_flags:
flags |= re.VERBOSE
return flags
示例4: str2float
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def str2float(string):
numeric_const_pattern = r"""
[-+]? # optional sign
(?:
(?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
|
(?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
)
# followed by optional exponent part if desired
(?: [Ee] [+-]? \d+ ) ?
"""
rx = re.compile(numeric_const_pattern, re.VERBOSE)
nb = rx.findall(string)
for i in enumerate(nb): nb[i[0]] = float(i[1])
return np.array(nb)
示例5: _parse
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def _parse(s):
rx_pattern = re.compile(r"""
\(CODE .*\)
|\(ID .*\d\)
""", re.VERBOSE|re.UNICODE)
s = re.sub(rx_pattern, '', s)
s = split(s, '\n')
fullPhrase = ""
# loop through the sentences and parse each sentence
# every time a new sentence marker is found
for sent in s:
if list(tokenize.regexp(sent, r'^\(')) != []:
fullPhrase = _strip_spaces(fullPhrase)
if fullPhrase != "":
yield fullPhrase
fullPhrase = sent
else:
fullPhrase += sent
# Get the last of the buffer and output a yield
fullPhrase = _strip_spaces(fullPhrase)
if fullPhrase != "":
yield fullPhrase
示例6: conllesp
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def conllesp():
from nltk.corpus import conll2002
de = """
.*
(
de/SP|
del/SP
)
"""
DE = re.compile(de, re.VERBOSE)
print()
print("Spanish CoNLL2002: de(ORG, LOC) -- just the first 10 clauses:")
print("=" * 45)
rels = [rel for doc in conll2002.chunked_sents('esp.train')
for rel in extract_rels('ORG', 'LOC', doc, corpus='conll2002', pattern = DE)]
for r in rels[:10]: print(clause(r, relsym='DE'))
print()
示例7: is_valid_ip
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def is_valid_ip(addr):
'''Validate an IPV4 address.
:param addr: IP address to validate.
:type addr: ``string``
:returns: True if is valid else False.
:rtype: ``bool``
'''
ip_rx = re.compile(r'''
^(((
[0-1]\d{2} # matches 000-199
| 2[0-4]\d # matches 200-249
| 25[0-5] # matches 250-255
| \d{1,2} # matches 0-9, 00-99
)\.){3}) # 3 of the preceding stanzas
([0-1]\d{2}|2[0-4]\d|25[0-5]|\d{1,2})$ # final octet
''', re.VERBOSE)
try:
return ip_rx.match(addr.strip())
except AttributeError:
# Value was not a string
return False
示例8: render_re
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def render_re(regex):
"""Renders a repr()-style value for a compiled regular expression."""
actual_flags = []
if regex.flags:
flags = [
(re.IGNORECASE, 'IGNORECASE'),
(re.LOCALE, 'LOCALE'),
(re.UNICODE, 'UNICODE'),
(re.MULTILINE, 'MULTILINE'),
(re.DOTALL, 'DOTALL'),
(re.VERBOSE, 'VERBOSE'),
]
for val, name in flags:
if regex.flags & val:
actual_flags.append(name)
if actual_flags:
return 're.compile(%r, %s)' % (regex.pattern, '|'.join(actual_flags))
else:
return 're.compile(%r)' % regex.pattern
示例9: __init__
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def __init__(self, spectree):
super().__init__(spectree)
from falcon.routing.compiled import _FIELD_PATTERN
self.FIELD_PATTERN = _FIELD_PATTERN
# NOTE from `falcon.routing.compiled.CompiledRouterNode`
self.ESCAPE = r'[\.\(\)\[\]\?\$\*\+\^\|]'
self.ESCAPE_TO = r'\\\g<0>'
self.EXTRACT = r'{\2}'
# NOTE this regex is copied from werkzeug.routing._converter_args_re and
# modified to support only int args
self.INT_ARGS = re.compile(r'''
((?P<name>\w+)\s*=\s*)?
(?P<value>\d+)\s*
''', re.VERBOSE)
self.INT_ARGS_NAMES = ('num_digits', 'min', 'max')
示例10: test_getstatus
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def test_getstatus(self):
# This pattern should match 'ls -ld /.' on any posix
# system, however perversely configured. Even on systems
# (e.g., Cygwin) where user and group names can have spaces:
# drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
# drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
# Note that the first case above has a space in the group name
# while the second one has a space in both names.
# Special attributes supported:
# + = has ACLs
# @ = has Mac OS X extended attributes
# . = has a SELinux security context
pat = r'''d......... # It is a directory.
[.+@]? # It may have special attributes.
\s+\d+ # It has some number of links.
[^/]* # Skip user, group, size, and date.
/\. # and end with the name of the file.
'''
with check_warnings((".*commands.getstatus.. is deprecated",
DeprecationWarning)):
self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE))
示例11: remove_stack_traces
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def remove_stack_traces(out):
# this regexp taken from Python 2.5's doctest
traceback_re = re.compile(r"""
# Grab the traceback header. Different versions of Python have
# said different things on the first traceback line.
^(?P<hdr> Traceback\ \(
(?: most\ recent\ call\ last
| innermost\ last
) \) :
)
\s* $ # toss trailing whitespace on the header.
(?P<stack> .*?) # don't blink: absorb stuff until...
^(?=\w) # a line *starts* with alphanum.
.*?(?P<exception> \w+ ) # exception name
(?P<msg> [:\n] .*) # the rest
""", re.VERBOSE | re.MULTILINE | re.DOTALL)
blocks = []
for block in blankline_separated_blocks(out):
blocks.append(traceback_re.sub(r"\g<hdr>\n...\n\g<exception>\g<msg>", block))
return "".join(blocks)
示例12: date_parser
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def date_parser(timestr, parserinfo=None, **kwargs):
"""
Uses dateutil.parser.parse, but also handles monthly dates of the form
1999m4, 1999:m4, 1999:mIV, 1999mIV and the same for quarterly data
with q instead of m. It is not case sensitive. The default for annual
data is the end of the year, which also differs from dateutil.
"""
flags = re.IGNORECASE | re.VERBOSE
if re.search(_q_pattern, timestr, flags):
y,q = timestr.replace(":","").lower().split('q')
month, day = _quarter_to_day[q.upper()]
year = int(y)
elif re.search(_m_pattern, timestr, flags):
y,m = timestr.replace(":","").lower().split('m')
month, day = _month_to_day[m.upper()]
year = int(y)
if _is_leap(y) and month == 2:
day += 1
elif re.search(_y_pattern, timestr, flags):
month, day = 12, 31
year = int(timestr)
else:
return to_datetime(timestr, **kwargs)
return datetime.datetime(year, month, day)
示例13: _encode_regex
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def _encode_regex(name, value, dummy0, dummy1):
"""Encode a python regex or bson.regex.Regex."""
flags = value.flags
# Python 2 common case
if flags == 0:
return b"\x0B" + name + _make_c_string_check(value.pattern) + b"\x00"
# Python 3 common case
elif flags == re.UNICODE:
return b"\x0B" + name + _make_c_string_check(value.pattern) + b"u\x00"
else:
sflags = b""
if flags & re.IGNORECASE:
sflags += b"i"
if flags & re.LOCALE:
sflags += b"l"
if flags & re.MULTILINE:
sflags += b"m"
if flags & re.DOTALL:
sflags += b"s"
if flags & re.UNICODE:
sflags += b"u"
if flags & re.VERBOSE:
sflags += b"x"
sflags += b"\x00"
return b"\x0B" + name + _make_c_string_check(value.pattern) + sflags
示例14: _split_string
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def _split_string(text):
"""
Split the combined (decoded) string into left, center and right parts
# See http://stackoverflow.com/questions/27711175/regex-with-multiple-optional-groups for discussion
"""
ITEM_REGEX = re.compile("""
(&L(?P<left>.+?))?
(&C(?P<center>.+?))?
(&R(?P<right>.+?))?
$""", re.VERBOSE | re.DOTALL)
m = ITEM_REGEX.match(text)
try:
parts = m.groupdict()
except AttributeError:
warn("""Cannot parse header or footer so it will be ignored""")
parts = {'left':'', 'right':'', 'center':''}
return parts
示例15: _parse_regex
# 需要導入模塊: import re [as 別名]
# 或者: from re import VERBOSE [as 別名]
def _parse_regex(self, string):
"""Parse output with grouped regex."""
results = []
self.regex = re.compile(self.pattern, re.VERBOSE)
for matches in self.regex.finditer(string):
results.append(matches.groupdict())
return results