我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用re.X。
def get_client_ip(request): access_route = get_access_route(request) if len(access_route) == 1: return access_route[0] expression = """ (^(?!(?:[0-9]{1,3}\.){3}[0-9]{1,3}$).*$)| # will match non valid ipV4 (^127\.0\.0\.1)| # will match 127.0.0.1 (^10\.)| # will match 10.0.0.0 - 10.255.255.255 IP-s (^172\.1[6-9]\.)| # will match 172.16.0.0 - 172.19.255.255 IP-s (^172\.2[0-9]\.)| # will match 172.20.0.0 - 172.29.255.255 IP-s (^172\.3[0-1]\.)| # will match 172.30.0.0 - 172.31.255.255 IP-s (^192\.168\.) # will match 192.168.0.0 - 192.168.255.255 IP-s """ regex = re.compile(expression, re.X) for ip in access_route: if not ip: # it's possible that the first value from X_FORWARDED_FOR # will be null, so we need to pass that value continue if regex.search(ip): continue else: return ip
def findHeadings(self, lines, struc): linenum=len(lines) level=struc[-1] _h_re=re.compile(self.textparser._h_re_base % level, re.X | re.M) hidx=[] for ii in xrange(linenum): if _h_re.match(lines[ii]): hidx.append(ii) hidx.append(linenum) groups=[[hidx[ii],hidx[ii+1]] for ii in xrange(len(hidx)-1)] result=[] for ii in groups: #--------Use heading line as container name-------- result.append(TextContainer(lines[ii[0]],struc,ii)) return result
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
def __init__(self, user, asset, role, login_type='ssh'): self.username = user.username self.asset_name = asset.hostname self.ip = None self.port = 22 self.ssh = None self.channel = None self.asset = asset self.user = user self.role = role self.remote_ip = '' self.login_type = login_type self.vim_flag = False self.vim_end_pattern = re.compile(r'\x1b\[\?1049', re.X) self.vim_data = '' self.stream = None self.screen = None self.__init_screen_stream()
def open(self): logger.debug('Websocket: Open exec request') role_name = self.get_argument('role', 'sb') self.remote_ip = self.request.headers.get("X-Real-IP") if not self.remote_ip: self.remote_ip = self.request.remote_ip logger.debug('Web????: ?????? %s' % role_name) self.role = get_object(PermRole, name=role_name) self.perm = get_group_user_perm(self.user) roles = self.perm.get('role').keys() if self.role not in roles: self.write_message('No perm that role %s' % role_name) self.close() self.assets = self.perm.get('role').get(self.role).get('asset') res = gen_resource({'user': self.user, 'asset': self.assets, 'role': self.role}) self.runner = MyRunner(res) message = '??????: ' + ', '.join([asset.hostname for asset in self.assets]) self.__class__.clients.append(self) self.write_message(message)
def _compile_patterns(self): """ Compiles the regexes based on the original implementation of the parser (``InventoryParser``) and sets the to ``self.patterns`` """ self.patterns['section'] = re.compile( r'''^\[ ([^:\]\s]+) # group name (?::(\w+))? # optional tag name : \] \s* # ignore trailing whitespace (?:\#.*)? # and/or a comment till the $ # end of the line ''', re.X ) self.patterns['groupname'] = re.compile( r'''^ ([^:\]\s]+) \s* # ignore trailing whitespace (?:\#.*)? # and/or a comment till the $ # end of the line ''', re.X )
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)
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)
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)
def __init__(self, stream): """Initialise Loader.""" try: self._root = os.path.split(stream.name)[0] except AttributeError: self._root = os.path.curdir super().__init__(stream) self.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.')) self.filenames = [os.path.abspath(stream.name)]
def _parse_rfc1738_args(url): pattern = re.compile(r''' (?P<name>[\w\+]+):// (?: (?P<username>[^:/]*) (?::(?P<password>.*))? @)? (?: (?: \[(?P<ipv6host>[^/]+)\] | (?P<ipv4host>[^/:]+) )? (?::(?P<port>[^/]*))? )? (?:/(?P<database>.*))? ''', re.X) m = pattern.match(url) if m is not None: components = m.groupdict() ipv6 = components.pop('ipv6host') ipv4 = components.pop('ipv4host') components['host'] = ipv4 or ipv6 return components else: raise ValueError('wrong url format')
def defSyntax(self): '''Define re patterns according to syntax.''' #------------------REGEX patterns------------------ if self.syntax=='markdown': self._img_re=re.compile('^(.*)!\\[(.+?)\\]\\((.+?)\\)', re.M | re.L) self._h_re_base = r''' (^(.+)[ \t]*\n(=+|-+)[ \t]*\n+) | (^(\#{%s}) # \1 = string of #'s [ \t]* (.+?) # \2 = Header text [ \t]* (?<!\\) # ensure not an escaped trailing '#' \#* # optional closing #'s (not counted) \n+ ) ''' self._all_h_re=re.compile(self._h_re_base %'1,6', re.X | re.M) elif self.syntax=='zim': self._img_re=re.compile('^(.*)\\{\\{(.+?)\\}\\}(.*)$', re.M | re.L) self._h_re_base = r''' ^(\={%s}) # \1 = string of ='s [ \t]* (.+?) # \2 = Header text [ \t]* \1 \n+ ''' self._all_h_re=re.compile(self._h_re_base %'1,6', re.X | re.M) else: raise Exception("Unknown syntax %s" %self.syntax) return
def createNoteBook(title,geeknote=None,verbose=True): #-------------------Trunc title------------------- title=title.strip() title=truncStr(title,MAX_NOTEBOOK_TITLE_LEN) #-------Make sure title doesnt start with #------- tp=textparse.TextParser('markdown') _h_re=re.compile(tp._h_re_base %'1,', re.X | re.M) m=_h_re.match(title) if m: title=m.group(6) #---------------------Connect--------------------- if geeknote is None: geeknote=GeekNoteConnector() geeknote.connectToEvertone() #-----------------Check if exists----------------- notebooks=geeknote.getEvernote().findNotebooks() out.preloader.stop() if not isinstance(title,unicode): title=unicode(title,'utf8') notebooks=[unicode(ii.name,'utf8') for ii in notebooks] if title in notebooks: out.successMessage('Notebook already exists.') return 0 else: out.preloader.setMessage("Creating notebook...") result = geeknote.getEvernote().createNotebook(name=title) if result: out.successMessage("Notebook has been successfully created.") return 0 else: out.failureMessage("Error while the process " "of creating the notebook.") return tools.exitErr()
def createNote(title,content,tags,notebook,geeknote=None,\ skipnotebook=False): #-------------------Trunc texts------------------- notebook=notebook.strip() notebook=truncStr(notebook,MAX_NOTEBOOK_TITLE_LEN) title=title.strip() title=truncStr(title,MAX_NOTE_TITLE_LEN) #-------Make sure title doesnt start with #------- tp=textparse.TextParser('markdown') _h_re=re.compile(tp._h_re_base %'1,', re.X | re.M) m=_h_re.match(title) if m: title=m.group(6) m=_h_re.match(notebook) if m: notebook=m.group(6) if tags is not None and len(tags.split(','))>=MAX_NOTE_TAGS: tags=u','.join(tags.split(',')[:MAX_NOTE_TAGS]) #---------------------Connect--------------------- if geeknote is None: geeknote=GeekNoteConnector() geeknote.connectToEvertone() #-----------------Create notebook----------------- if not skipnotebook: result=createNoteBook(notebook,geeknote) if skipnotebook or result==0: #----------------------Write---------------------- inputdata=geeknote._parseInput(title,content,tags,notebook,None) out.preloader.setMessage('Creating note...') result=bool(geeknote.getEvernote().createNote(**inputdata)) if result: out.successMessage("Note has been successfully saved.") else: out.failureMessage("Error while saving the note.")