Python re.VERBOSE Examples
The following are 30
code examples of re.VERBOSE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
re
, or try the search function
.
Example #1
Source File: magic_check_fn.py From recipes-py with Apache License 2.0 | 10 votes |
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
Example #2
Source File: m_siesta_ion_xml.py From pyscf with Apache License 2.0 | 7 votes |
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
Example #3
Source File: regex.py From recruit with Apache License 2.0 | 6 votes |
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
Example #4
Source File: __init__.py From vnpy_crypto with MIT License | 6 votes |
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
Example #5
Source File: ip_math.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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
Example #6
Source File: header_footer.py From vnpy_crypto with MIT License | 6 votes |
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
Example #7
Source File: ip_math.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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
Example #8
Source File: support.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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
Example #9
Source File: relextract.py From razzy-spinner with GNU General Public License v3.0 | 6 votes |
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()
Example #10
Source File: falcon_plugin.py From spectree with Apache License 2.0 | 6 votes |
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')
Example #11
Source File: plugintest.py From locality-sensitive-hashing with MIT License | 6 votes |
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)
Example #12
Source File: regex.py From vnpy_crypto with MIT License | 6 votes |
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
Example #13
Source File: ycoe.py From razzy-spinner with GNU General Public License v3.0 | 6 votes |
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
Example #14
Source File: m_siesta_ion_xml.py From pyscf with Apache License 2.0 | 6 votes |
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)
Example #15
Source File: datetools.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #16
Source File: support.py From jawfish with MIT License | 6 votes |
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
Example #17
Source File: test_commands.py From ironpython2 with Apache License 2.0 | 6 votes |
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))
Example #18
Source File: __init__.py From ironpython2 with Apache License 2.0 | 6 votes |
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
Example #19
Source File: plugintest.py From locality-sensitive-hashing with MIT License | 5 votes |
def simplify_warnings(out): warn_re = re.compile(r""" # Cut the file and line no, up to the warning name ^.*:\d+:\s (?P<category>\w+): \s+ # warning category (?P<detail>.+) $ \n? # warning message ^ .* $ # stack frame """, re.VERBOSE | re.MULTILINE) return warn_re.sub(r"\g<category>: \g<detail>", out)
Example #20
Source File: feed_service.py From C-3PO with MIT License | 5 votes |
def get_yt_id(url): pattern = re.compile( r""" (https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\. (com|be)/(watch\?v=|embed/|v/|.+\?v=)? (?P<id>[A-Za-z0-9\-=_]{11})""", re.VERBOSE, ) match = pattern.match(url) if not match: return None return match.group("id")
Example #21
Source File: scp_scf_ICESat2_files.py From read-ICESat-2 with MIT License | 5 votes |
def scp_pull_file(client_ftp, scf_client_ftp, transfer_file, remote_dir, scf_outgoing, CLOBBER=False, VERBOSE=False, LIST=False, MODE=0o775): #-- remote and scf outgoing versions of file remote_file = os.path.join(remote_dir,transfer_file) outgoing_file = posixpath.join(scf_outgoing,transfer_file) #-- get access and modification time of remote file outgoing_atime = scf_client_ftp.stat(outgoing_file).st_atime outgoing_mtime = scf_client_ftp.stat(outgoing_file).st_mtime #-- check if remote file is newer than the local file TEST = False OVERWRITE = 'clobber' if (transfer_file in client_ftp.listdir(remote_dir)): remote_mtime = client_ftp.stat(remote_file).st_mtime #-- if remote file is newer: overwrite the local file if (even(outgoing_mtime) > even(remote_mtime)): TEST = True OVERWRITE = 'overwrite' else: TEST = True OVERWRITE = 'new' #-- if file does not exist locally, is to be overwritten, or CLOBBER is set if TEST or CLOBBER: if VERBOSE or LIST: print('{0} --> '.format(outgoing_file)) print('\t{0} ({1})\n'.format(remote_file,OVERWRITE)) #-- if not only listing files if not LIST: #-- load scf file contents to BytesIO object fileobj = io.BytesIO() scf_client_ftp.getfo(outgoing_file, fileobj) #-- rewind retrieved binary to start of file fileobj.seek(0) #-- copy BytesIO object to remote server client_ftp.putfo(fileobj, remote_file) #-- change the permissions level of the transported file to MODE client_ftp.chmod(remote_file, MODE) #-- keep modification and access time of scf file client_ftp.utime(remote_file, (outgoing_atime, outgoing_mtime)) #-- PURPOSE: rounds a number to an even number less than or equal to original
Example #22
Source File: scp_ICESat2_files.py From read-ICESat-2 with MIT License | 5 votes |
def scp_pull_file(client, client_ftp, transfer_file, local_dir, remote_dir, CLOBBER=False, VERBOSE=False, LIST=False, MODE=0o775): #-- local and remote versions of file local_file = os.path.join(local_dir,transfer_file) remote_file = posixpath.join(remote_dir,transfer_file) #-- check if remote file is newer than the local file TEST = False OVERWRITE = 'clobber' if os.access(local_file, os.F_OK): local_mtime = os.stat(local_file).st_mtime remote_mtime = client_ftp.stat(remote_file).st_mtime #-- if remote file is newer: overwrite the local file if (even(remote_mtime) > even(local_mtime)): TEST = True OVERWRITE = 'overwrite' else: TEST = True OVERWRITE = 'new' #-- if file does not exist locally, is to be overwritten, or CLOBBER is set if TEST or CLOBBER: if VERBOSE or LIST: print('{0} --> '.format(remote_file)) print('\t{0} ({1})\n'.format(local_file,OVERWRITE)) #-- if not only listing files if not LIST: #-- copy local files from remote server with scp.SCPClient(client.get_transport(), socket_timeout=20) as s: s.get(remote_file, local_path=local_file, preserve_times=True) #-- change the permissions level of the transported file to MODE os.chmod(local_file, MODE) #-- PURPOSE: rounds a number to an even number less than or equal to original
Example #23
Source File: symbolic_ICESat2_files.py From read-ICESat-2 with MIT License | 5 votes |
def symbolic_ICESat2_files(base_dir, scf_incoming, scf_outgoing, PRODUCT, RELEASE, VERSIONS, GRANULES, CYCLES, TRACKS, VERBOSE=False, MODE=0o775): #-- find ICESat-2 HDF5 files in the subdirectory for product and release TRACKS = np.arange(1,1388) if not np.any(TRACKS) else TRACKS CYCLES = np.arange(1,10) if not np.any(CYCLES) else CYCLES GRANULES = np.arange(1,15) if not np.any(GRANULES) else GRANULES VERSIONS = np.arange(1,10) if not np.any(VERSIONS) else VERSIONS regex_track = '|'.join(['{0:04d}'.format(T) for T in TRACKS]) regex_cycle = '|'.join(['{0:02d}'.format(C) for C in CYCLES]) regex_granule = '|'.join(['{0:02d}'.format(G) for G in GRANULES]) regex_version = '|'.join(['{0:02d}'.format(V) for V in VERSIONS]) #-- compile regular expression operator for extracting data from files args = (PRODUCT,regex_track,regex_cycle,regex_granule,RELEASE,regex_version) regex_pattern = ('(processed_)?({0})(-\d{{2}})?_(\d{{4}})(\d{{2}})(\d{{2}})' '(\d{{2}})(\d{{2}})(\d{{2}})_({1})({2})({3})_({4})_({5})(.*?).h5$') rx = re.compile(regex_pattern.format(*args),re.VERBOSE) #-- find files within scf_outgoing file_transfers = [f for f in os.listdir(scf_outgoing) if rx.match(f)] for f in sorted(file_transfers): #-- extract parameters from file SUB,PRD,HEM,YY,MM,DD,HH,MN,SS,TRK,CYC,GRN,RL,VRS,AUX=rx.findall(f).pop() #-- put symlinks in directories similar to NSIDC #-- check if data directory exists and recursively create if not local_dir = os.path.join(base_dir,'{0}.{1}.{2}'.format(YY,MM,DD)) os.makedirs(local_dir,MODE) if not os.path.exists(local_dir) else None #-- print original and symbolic link of file if VERBOSE: print('{0} -->'.format(os.path.join(scf_outgoing,f))) print('\t{0}'.format(os.path.join(local_dir,f))) #-- create symbolic link of file from scf_outgoing to local os.symlink(os.path.join(scf_outgoing,f), os.path.join(local_dir,f)) #-- run main program
Example #24
Source File: linters.py From ciocheck with MIT License | 5 votes |
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
Example #25
Source File: scanner.py From python-spark with MIT License | 5 votes |
def __init__(self): pattern = self.reflect() self.pos = 0 self.re = re.compile(pattern, re.VERBOSE) self.index2func = {} for name, number in self.re.groupindex.items(): self.index2func[number-1] = getattr(self, 't_' + name)
Example #26
Source File: test_regressions.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_ipy3_gh546(self): """https://github.com/IronLanguages/ironpython3/issues/546""" import re _SECT_TMPL = r""" \[ # [ (?P<header>[^]]+) # very permissive! \] # ] """ SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE) string = "[some header]" match = SECTCRE.match(string) self.assertEqual(match.span(), (0, len(string)))
Example #27
Source File: test_re.py From ironpython2 with Apache License 2.0 | 5 votes |
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)
Example #28
Source File: string.py From ironpython2 with Apache License 2.0 | 5 votes |
def __init__(cls, name, bases, dct): super(_TemplateMetaclass, cls).__init__(name, bases, dct) if 'pattern' in dct: pattern = cls.pattern else: pattern = _TemplateMetaclass.pattern % { 'delim' : _re.escape(cls.delimiter), 'id' : cls.idpattern, } cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
Example #29
Source File: input.py From bob with GNU General Public License v3.0 | 5 votes |
def __init__(self, scriptLanguage, fileLoader, baseDir, varBase, sourceName): self.__pattern = re.compile(r""" \$<(?: (?P<escaped>\$) | (?P<named>[<'][^'>]+)['>]> | (?P<braced>[<'][^'>]+)['>]> | (?P<invalid>) ) """, re.VERBOSE) self.__resolverClass = scriptLanguage.Resolver self.__baseDir = baseDir self.__varBase = re.sub(r'[^a-zA-Z0-9_]', '_', varBase, flags=re.DOTALL) self.__fileLoader = fileLoader self.__sourceName = sourceName
Example #30
Source File: string.py From meddle with MIT License | 5 votes |
def __init__(cls, name, bases, dct): super(_TemplateMetaclass, cls).__init__(name, bases, dct) if 'pattern' in dct: pattern = cls.pattern else: pattern = _TemplateMetaclass.pattern % { 'delim' : _re.escape(cls.delimiter), 'id' : cls.idpattern, } cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)