我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用regex.VERBOSE。
def parse_primary_source(text): """given a primary source text reference it returns the abbrev of the primary source """ psource = "" ref_regex = regex.compile(r""" (?P<psource>(\w+\.?\s?)+) (\(?\d\)\s?)+ """, regex.VERBOSE) res = regex.search(ref_regex, text) if res: if res.group(psource): return res.group(psource) else: return None else: return None
def test_detect_verbose(self): """Test verbose.""" pattern = bregex.compile_search( r''' This is a # \Qcomment\E This is not a \# \Qcomment\E This is not a [#\ ] \Qcomment\E This is not a [\#] \Qcomment\E This\ is\ a # \Qcomment\E ''', regex.VERBOSE ) self.assertEqual( pattern.pattern, r''' This is a # \Qcomment\E This is not a \# comment This is not a [#\ ] comment This is not a [\#] comment This\ is\ a # \Qcomment\E ''' )
def test_detect_verbose_v1(self): """Test verbose.""" pattern = bregex.compile_search( r'''(?V1) This is a # \Qcomment\E This is not a \# \Qcomment\E This is not a [#\ ] \Qcomment\E This is not a [\#] \Qcomment\E This\ is\ a # \Qcomment\E ''', regex.VERBOSE ) self.assertEqual( pattern.pattern, r'''(?V1) This is a # \Qcomment\E This is not a \# comment This is not a [#\ ] comment This is not a [\#] comment This\ is\ a # \Qcomment\E ''' )
def test_constants(self): if regex.I != regex.IGNORECASE: self.fail() if regex.L != regex.LOCALE: self.fail() if regex.M != regex.MULTILINE: self.fail() if regex.S != regex.DOTALL: self.fail() if regex.X != regex.VERBOSE: self.fail()
def test_unicode_and_verbose_flag(self): """Test that VERBOSE and UNICODE together come through.""" pattern = bregex.compile_search(r'Some pattern', flags=bregex.VERBOSE | bregex.UNICODE) self.assertTrue(pattern.flags & bregex.UNICODE and pattern.flags & bregex.VERBOSE)
def test_version0_and_verbose_flag(self): """Test that VERBOSE and V0 together come through.""" pattern = bregex.compile_search(r'Some pattern', flags=bregex.VERBOSE | bregex.V0) self.assertTrue(pattern.flags & bregex.V0 and pattern.flags & bregex.VERBOSE)
def test_version1_and_verbose_flag(self): """Test that VERBOSE and V1 together come through.""" pattern = bregex.compile_search(r'Some pattern', flags=bregex.VERBOSE | bregex.V1) self.assertTrue(pattern.flags & bregex.V1 and pattern.flags & bregex.VERBOSE)
def eval_escapes(s): """ Given a string, evaluate escape sequences starting with backslashes as they would be evaluated in Python source code. For a list of these sequences, see: https://docs.python.org/3/reference/lexical_analysis.html This is not the same as decoding the whole string with the 'unicode-escape' codec, because that provides no way to handle non-ASCII characters that are literally present in the string. """ # by Rob Speer escape_sequence_re = re.compile( r''' ( \\U........ # 8-digit Unicode escapes | \\u.... # 4-digit Unicode escapes | \\x.. # 2-digit Unicode escapes | \\[0-7]{1,3} # Octal character escapes | \\N\{[^}]+\} # Unicode characters by name | \\[\\'"abfnrtv] # Single-character escapes )''', re.UNICODE | re.VERBOSE ) def decode_match(match): return codecs.decode(match.group(0), 'unicode-escape') return escape_sequence_re.sub(decode_match, s)