本文整理匯總了Python中re.X屬性的典型用法代碼示例。如果您正苦於以下問題:Python re.X屬性的具體用法?Python re.X怎麽用?Python re.X使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類re
的用法示例。
在下文中一共展示了re.X屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getmatch
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def getmatch(self, haystack):
if not isinstance(haystack, basestring):
return None
flags = 0
if self.flags is not None:
if "i" in self.flags or "I" in self.flags:
flags |= re.I
if "l" in self.flags or "L" in self.flags:
flags |= re.L
if "m" in self.flags or "M" in self.flags:
flags |= re.M
if "s" in self.flags or "S" in self.flags:
flags |= re.S
if "u" in self.flags or "U" in self.flags:
flags |= re.U
if "x" in self.flags or "X" in self.flags:
flags |= re.X
if re.match(self.pattern, haystack, flags=flags) is None:
return None
elif self.to is None:
return Match(haystack, haystack)
else:
return Match(haystack, re.sub(self.pattern, self.to, haystack, flags=flags))
示例2: make_loader
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def make_loader(*tags_to_remove: str) -> Type[yaml.SafeLoader]:
"""Create a YAML loader, that doesn't parse specific tokens into Python objects."""
cls: Type[yaml.SafeLoader] = type("YAMLLoader", (SafeLoader,), {})
cls.yaml_implicit_resolvers = {
key: [(tag, regexp) for tag, regexp in mapping if tag not in tags_to_remove]
for key, mapping in cls.yaml_implicit_resolvers.copy().items()
}
# Fix pyyaml scientific notation parse bug
# See PR: https://github.com/yaml/pyyaml/pull/174 for upstream fix
cls.add_implicit_resolver( # type: ignore
"tag:yaml.org,2002:float",
re.compile(
r"""^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\.[0-9_]+(?:[eE][-+]?[0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
|[-+]?\.(?:inf|Inf|INF)
|\.(?:nan|NaN|NAN))$""",
re.X,
),
list("-+0123456789."),
)
return cls
示例3: match_text
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def match_text(self):
match = self.match(r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
# comment preceded by a
# consumed newline and whitespace
|
(?=\${) # an expression
|
(?=</?[%&]) # a substitution or block or call start or end
# - don't consume
|
(\\\r?\n) # an escaped newline - throw away
|
\Z # end of string
)""", re.X | re.S)
if match:
text = match.group(1)
if text:
self.append_node(parsetree.Text, text)
return True
else:
return False
示例4: parse_args
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def parse_args(parser):
args = parser.parse_args()
if args.config_file:
loader = yaml.SafeLoader
loader.add_implicit_resolver(
u'tag:yaml.org,2002:float',
re.compile(u'''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.'))
data = yaml.load(args.config_file, Loader=loader)
delattr(args, 'config_file')
arg_dict = args.__dict__
# print(len(list(arg_dict.keys())))
# print(len(list(data.keys())))
for key, value in arg_dict.items():
default_arg = parser.get_default(key)
if arg_dict[key] == default_arg and key in data:
arg_dict[key] = data[key]
return args
示例5: _do_code_blocks
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def _do_code_blocks(self, text):
"""Process Markdown `<pre><code>` blocks."""
code_block_re = re.compile(r'''
(?:\n\n|\A\n?)
( # $1 = the code block -- one or more lines, starting with a space/tab
(?:
(?:[ ]{%d} | \t) # Lines must start with a tab or a tab-width of spaces
.*\n+
)+
)
((?=^[ ]{0,%d}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
# Lookahead to make sure this block isn't already in a code block.
# Needed when syntax highlighting is being used.
(?![^<]*\</code\>)
''' % (self.tab_width, self.tab_width),
re.M | re.X)
return code_block_re.sub(self._code_block_sub, text)
示例6: _xml_oneliner_re_from_tab_width
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def _xml_oneliner_re_from_tab_width(tab_width):
"""Standalone XML processing instruction regex."""
return re.compile(r"""
(?:
(?<=\n\n) # Starting after a blank line
| # or
\A\n? # the beginning of the doc
)
( # save in $1
[ ]{0,%d}
(?:
<\?\w+\b\s+.*?\?> # XML processing instruction
|
<\w+:\w+\b\s+.*?/> # namespaced single tag
)
[ \t]*
(?=\n{2,}|\Z) # followed by a blank line or end of document
)
""" % (tab_width - 1), re.X)
示例7: _hr_tag_re_from_tab_width
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def _hr_tag_re_from_tab_width(tab_width):
return re.compile(r"""
(?:
(?<=\n\n) # Starting after a blank line
| # or
\A\n? # the beginning of the doc
)
( # save in \1
[ ]{0,%d}
<(hr) # start tag = \2
\b # word break
([^<>])*? #
/?> # the matching end tag
[ \t]*
(?=\n{2,}|\Z) # followed by a blank line or end of document
)
""" % (tab_width - 1), re.X)
示例8: __bytes__
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def __bytes__(self):
"""
:return:
A byte string
"""
if self.contents is None:
return b''
if self._bytes is None:
if not self._indefinite:
self._bytes, self._unused_bits = self._as_chunk()[0]
else:
chunks = self._merge_chunks()
self._unused_bits = ()
for chunk in chunks:
if self._unused_bits:
# Disallowed by X.690 §8.6.4
raise ValueError('Only last chunk in a bit string may have unused bits')
self._unused_bits = chunk[1]
self._bytes = b''.join(chunk[0] for chunk in chunks)
return self._bytes
示例9: test_basic_re_sub
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def test_basic_re_sub(self):
self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
'9.3 -3 24x100y')
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
'9.3 -3 23x99y')
self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
self.assertEqual(re.sub('.', r"\n", 'x'), '\n')
s = r"\1\1"
self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')
self.assertEqual(re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a'),
'\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'), '\t\n\v\r\f\a')
self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'),
(chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest')
示例10: test_constants
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def test_constants(self):
self.assertEqual(re.I, re.IGNORECASE)
self.assertEqual(re.L, re.LOCALE)
self.assertEqual(re.M, re.MULTILINE)
self.assertEqual(re.S, re.DOTALL)
self.assertEqual(re.X, re.VERBOSE)
示例11: test_flags
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def test_flags(self):
for flag in [re.I, re.M, re.X, re.S, re.L]:
self.assertNotEqual(re.compile('^pattern$', flag), None)
示例12: __escape
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def __escape(self, m):
codepoint = ord(m.group())
try:
return self.codepoint2entity[codepoint]
except (KeyError, IndexError):
return "&#x%X;" % codepoint
示例13: match_text
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def match_text(self):
match = self.match(
r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
# comment preceded by a
# consumed newline and whitespace
|
(?=\${) # an expression
|
(?=</?[%&]) # a substitution or block or call start or end
# - don't consume
|
(\\\r?\n) # an escaped newline - throw away
|
\Z # end of string
)""",
re.X | re.S,
)
if match:
text = match.group(1)
if text:
self.append_node(parsetree.Text, text)
return True
else:
return False
示例14: _rfc_1738_quote
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def _rfc_1738_quote(text):
return re.sub(r'[:@/]', lambda m: "%%%X" % ord(m.group(0)), text)
示例15: __escape
# 需要導入模塊: import re [as 別名]
# 或者: from re import X [as 別名]
def __escape(self, m):
codepoint = ord(m.group())
try:
return self.codepoint2entity[codepoint]
except (KeyError, IndexError):
return '&#x%X;' % codepoint