def countAllUrl(aName): result_set = set() # print('before start') tFile = open(aName, 'r') count = 0 pattern = '(GET|POST|PUT|DELETE) (.+) HTTP' sPattern = '(.+)\?' for line in tFile: match = re.search(pattern, line) if match: mystr = match.group(2) index = mystr.find('server.php') if index > 0: smatch = re.search(sPattern, mystr) if smatch: result_set.add(smatch.group(1)) else: result_set.add(match.group(2)) count += 1 # if count==1000: # break # print(line) # tFile.read() tFile.close() # print(result_set) for i in result_set: # print(i) pass return count
def three_kind(hand): match = search(r"(.). \1. \1. ?", hand) if match: hand = sub(match.group(), '', hand) hand = hand.strip() if not match.group()[0] == ' ': hand += ' ' return hand + match.group() return False
def four_kind(hand): match = search(r"(.).( \1.){3} ?", hand) if match: hand = sub(match.group(), '', hand) hand = hand.strip() if not match.group()[0] == ' ': hand += ' ' return hand + match.group() return False
def royal_flush(hand): match = search(r"T(.) J\1 Q\1 K\1 A\1", hand) if match: hand = sub(match.group(), '', hand) hand = hand.strip() if not match.group()[0] == ' ': hand += ' ' return hand + match.group() return False
def one_pair(hand): match = search(r" ?(.). \1.", hand) if match: hand = sub(match.group(), '', hand) hand = hand.strip() if not match.group()[0] == ' ': hand += ' ' return hand + match.group() return False
def pathstr2mplpaths(pathstr): # TODO: check paths = [] offset = np.array([0,0]) for match in finditer("([Mm])[^Mm]+", pathstr): shift = np.array([0,0]) if match.group(1).isupper() else offset mplpath = splinestr2mplpath(match.group(0), shift) if len(mplpath) > 0: # print mplpath.vertices paths.append(mplpath) offset = paths[-1].vertices[0,:] return paths
def _to_object(match): ctype = match.group(1) antecedent = match.group(2) consequent = match.group(3) support = float(match.group(4)) confidence = float(match.group(5)) return IRule( Rule(URIRef(ctype), _mkCondition(antecedent), _mkConsequent(consequent)), IRule.Measure(support), IRule.Measure(confidence))
def n(self): """ Number of atoms added/removed from system. This is a dictionary. """ from re import match if self.is_vacancy: return {match("vacancy_([A-Z][a-z])?", self.name).group(1): -1} elif self.is_interstitial: return {match("[A-Z][a-z]?_interstitial_(\S+)", self.name).group(1): -1} else: found = match("([A-Z][a-z])?_on_([A-Z][a-z])?", self.name) return {match.group(1): 1, match.group(2): -1}
def n(self): """ Number of atoms added/removed from system. This is a dictionary. """ from re import match if self.is_vacancy: return {match("vacancy_([A-Z][a-z])?", self.name).group(1): -1} elif self.is_interstitial: return { match("[A-Z][a-z]?_interstitial_(\S+)", self.name).group(1): -1 } else: found = match("([A-Z][a-z])?_on_([A-Z][a-z])?", self.name) return {match.group(1): 1, match.group(2): -1}
def update_log(logline): if BOT_TOKEN: send_telegram_log(logline) n = 0 for line in fileinput.input(os.path.join(SCRIPT_DIR, 'script.log'), inplace=1): match = search( r'(?:\[[0-9\.\s\:]+\]\s' + escape(logline) + r')(?:\s\{([0-9]+)(?:\}))?', line) if match: n = int(match.group(1)) if match.group(1) else 1 else: print(line, end='') last_line = '[{}] {} {{{}}}\n'.format(get_current_datetime(), logline, n + 1) if n > 0 else '[{}] {}\n'.format( get_current_datetime(), logline) with open(os.path.join(SCRIPT_DIR, 'script.log'), 'a') as f: f.write(last_line)
def run_command(self, args, data, channel_name=None): response = self.response response = response.replace("%name%", data["user_name"]) try: response = sub( r"%arg(\d+)%", lambda match: args[int(match.group(1))], response ) except IndexError: return "Not enough arguments!" response = response.replace("%args%", ' '.join(args[1:])) self.calls += 1 session.commit() response = response.replace("%count%", str(self.calls)) response = response.replace( "%channel%", channel_name if channel_name else data["id"] ) return response.split('\\n', 2)
def run_command(self, args, data, channel_name=None): response = self.response response = response.replace("%name%", data["user_name"]) try: response = sub( "%arg(\d+)%", lambda match: args[int(match.group(1))], response ) except IndexError: return "Not enough arguments!" response = response.replace("%args%", ' '.join(args[1:])) self.calls += 1 session.commit() response = response.replace("%count%", str(self.calls)) response = response.replace( "%channel%", channel_name if channel_name else data["id"] ) return response
def __call__(self, opcode_arg): """Fill in the opcode_arg property. Return true if matched. Do nothing if not matched.""" match = self.regex.match(opcode_arg.syntax_token) if not match: return None if self.bitfield_symbol is not None: if type(self.bitfield_symbol) is int: bf_sym = match.group(self.bitfield_symbol) else: bf_sym = self.bitfield_symbol opcode_arg.bitfield_symbol = bf_sym if self.arg_type is not None: opcode_arg.arg_type = self.arg_type opcode_arg.typemacro = self.macroname() if self.modifier is not None: self.modifier(opcode_arg) return True
def p1(filename): tokens = [] line_tokens = [] comment_depth = 0 with open(filename, 'r') as f: for line in f: line = line.strip() #print 'INPUT: ' + line i = 0 while i < len(line): match = None defs = token_defs if comment_depth == 0 else comment_defs for definition in defs: regex, label = definition match = regex.match(line, i) if match: text = match.group(0) if label: if label == INCOMMENT: comment_depth += 1 elif label == OUTCOMMENT: comment_depth -= 1 else: line_tokens.append(text if text in keywords or label == SYMBOL else label) break i = match.end(0) #for token in line_tokens: print token tokens.extend(line_tokens) del line_tokens[:] tokens.append('$') # prep as stack for parsing tokens.reverse() return tokens
def host_gcc_version(d, taskcontextonly=False): import re, subprocess if taskcontextonly and d.getVar('BB_WORKERCONTEXT') != '1': return compiler = d.getVar("BUILD_CC") # Get rid of ccache since it is not present when parsing. if compiler.startswith('ccache '): compiler = compiler[7:] try: env = os.environ.copy() env["PATH"] = d.getVar("PATH") output = subprocess.check_output("%s --version" % compiler, \ shell=True, env=env, stderr=subprocess.STDOUT).decode("utf-8") except subprocess.CalledProcessError as e: bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) match = re.match(r".* (\d\.\d)\.\d.*", output.split('\n')[0]) if not match: bb.fatal("Can't get compiler version from %s --version output" % compiler) version = match.group(1) return "-%s" % version if version in ("4.8", "4.9") else ""
def get_fastq_file_ending(self, filename): """Extract file ending from FASTQ filename""" if utils.is_fastq(filename) is False: raise ValueError("{0} is not a valid FASTQ file!".format(filename)) pattern = "^.*(\.fastq(\.gz)?)$" match = search(pattern, filename) return match.group(1)
def get_host_compiler_version(d, taskcontextonly=False): import re, subprocess if taskcontextonly and d.getVar('BB_WORKERCONTEXT') != '1': return compiler = d.getVar("BUILD_CC") # Get rid of ccache since it is not present when parsing. if compiler.startswith('ccache '): compiler = compiler[7:] try: env = os.environ.copy() # datastore PATH does not contain session PATH as set by environment-setup-... # this breaks the install-buildtools use-case # env["PATH"] = d.getVar("PATH") output = subprocess.check_output("%s --version" % compiler, \ shell=True, env=env, stderr=subprocess.STDOUT).decode("utf-8") except subprocess.CalledProcessError as e: bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) match = re.match(r".* (\d+\.\d+)\.\d+.*", output.split('\n')[0]) if not match: bb.fatal("Can't get compiler version from %s --version output" % compiler) version = match.group(1) return compiler, version
def f_par(self, post, mode): global CRLF post = post + '\r\n\r\n' post = re.sub(r'\r?\n\r?\n\r?\n\r?\n', r'\r\n\r\n', post, re.I | re.M | re.S) h_p = r'.*?(?=(\r?\n\r?\n))' d_p = r'((?<=(\n\n))|(?<=(\r\n\r\n))).*' hd = search(h_p, post, re.I | re.M | re.S) bd = search(d_p, post, re.I | re.M | re.S) b = hd.group() bd = bd.group().rstrip() hrs = [] regex = re.compile(r'^.*$', re.I | re.M) for match in regex.finditer(b): hrs.append(match.group()) f_l = hrs[0] del hrs[0] length = len(hrs) h_t = {} try: for count in range(0, length): sp = hrs[count].split(':', 1) h_t[sp[0].rstrip()] = sp[1] except IndexError: print colored("\nHttp request not in proper format.", 'white', 'on_red') print "Kindly check the http request file.\nIf the file contains burplog or burp history, use -b option instead.\nExiting..." sys.exit(0) method = f_l.split(' ', 1)[0] if 'Host' in h_t: url1 = h_t['Host'].replace("http://", '').replace("https://", '') h_t['Host'] = url1 url = url1.lstrip().rstrip() + f_l.split()[1].lstrip() elif 'host' in h_t: url1 = h_t['host'].replace("http://", '').replace("https://", '') h_t['host'] = url1 url = url1.lstrip().rstrip() + f_l.split()[1].lstrip() else: print colored("\nHost header not found in http request file.", 'white', 'on_red') print colored("\nExiting...", 'red') sys.exit(0) mode = mode.lower() if mode == 'http': url = 'http://' + url url = url.replace(' ', '+') url = url.replace('#', r'%23') elif mode == 'https': url = 'https://' + url url = url.replace(' ', '+') url = url.replace('#', '%23') else: print colored("Please select mode between HTTP or HTTPS", 'white', 'on_red') sys.exit(0) return h_t, bd, method, url
def _encode_basestring(value): if isinstance(value, str) and \ json.encoder.HAS_UTF8.search(value) is not None: value = value.decode("utf-8", "replace") # ensure the decode succeeds. replace = lambda match: json.encoder.ESCAPE_DCT[match.group(0)] return u'"%s"' % json.encoder.ESCAPE.sub(replace, value)
def rule(match): nonlocal num_md_sequences name = match.group(1) blocks = match.group(2).split('\n\n---\n\n') if name == 'markdown': name = f'markdown-{num_md_sequences}' num_md_sequences += 1 contents = [] captions = [] for block in blocks: content, caption = block.split('\n\n-\n\n') contents.append(content) captions.append(caption) load_pyagram_captions(assets_dir, f'{name}-contents', contents) load_pyagram_captions(assets_dir, f'{name}-captions', captions) return f"{{{{ macros.md_slider('{name}', md_sequence(resource, '{name}')) }}}}" else: captions = blocks load_pyagram_captions(assets_dir, name, captions) return f"{{{{ macros.slider('{name}', sequence(resource, '{name}')) }}}}"
def f_par(self,post,mode): global CRLF post = post + '\r\n\r\n' post = re.sub(r'\r?\n\r?\n\r?\n\r?\n',r'\r\n\r\n',post,re.I|re.M|re.S) h_p = r'.*?(?=(\r?\n\r?\n))' d_p = r'((?<=(\n\n))|(?<=(\r\n\r\n))).*' hd = search(h_p,post,re.I|re.M|re.S) bd = search(d_p,post,re.I|re.M|re.S) b=hd.group() bd = bd.group().rstrip() hrs = [] regex = re.compile(r'^.*$',re.I|re.M) for match in regex.finditer(b): hrs.append(match.group()) f_l = hrs[0] del hrs[0] length = len(hrs) h_t ={} try: for count in range(0,length): sp = hrs[count].split(':',1) h_t[sp[0].rstrip()] = sp[1] except IndexError: print colored("\nHttp request not in proper format.",'white','on_red') print "Kindly check the http request file.\nIf the file contains burplog or burp history, use -b option instead.\nExiting..." sys.exit(0) method = f_l.split(' ',1)[0] if 'Host' in h_t: url1 = h_t['Host'].replace("http://",'').replace("https://",'') h_t['Host'] = url1 url = url1.lstrip().rstrip()+f_l.split()[1].lstrip() elif 'host' in h_t: url1 = h_t['host'].replace("http://",'').replace("https://",'') h_t['host'] = url1 url = url1.lstrip().rstrip()+f_l.split()[1].lstrip() else: print colored("\nHost header not found in http request file.",'white','on_red') print colored("\nExiting...",'red') sys.exit(0) mode = mode.lower() if mode == 'http': url = 'http://'+ url url = url.replace(' ','+') url = url.replace('#',r'%23') elif mode =='https': url = 'https://'+ url url = url.replace(' ','+') url = url.replace('#','%23') else: print colored("Please select mode between HTTP or HTTPS",'white','on_red') sys.exit(0) return h_t,bd,method,url
def get_resource_xml(content): if content: mathrul = "attr|iD|style|string|dimen|color|array|drawable|layout|anim|integer|animator|interpolator|transition|raw" output = re.finditer(r'resource (0x7f[0-f]{6}).*(' + mathrul + ')\/(.+)\:.*', content) idsnum = [] publicxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" idsxml = publicxml for match in output: iD = str(match.group(1)) if iD not in idsnum: idsnum.append(iD) publicinfo = " <public type=\"" + str(match.group(2)) + "\" name=\"" + str(match.group(3)) + "\" iD=\"" + str(match.group(1)) + "\" />\n" publicxml += publicinfo if str(match.group(2)) == "iD": idinfo = " <item type=\"iD\" name=\"" + str(match.group(3)) + "\" />\n" idsxml += idinfo publicxml += "</resources>" idsxml += "</resources>" return (publicxml, idsxml)
def add_reaction_from_str(model, reaction_str): """ Parse a reaction from a string and add it to the model. Arguments: model : StoichiometricModel -- model reaction_str: str -- string representation a the reaction """ match = regex_reaction.match(reaction_str) if match: reaction_id = match.group('reaction_id') reversible = match.group('direction') == '<->' substrates = match.group('substrates') products = match.group('products') # add reaction to model only if it is not part of the model if reaction_id not in model.reactions: if substrates or products: reaction = Reaction(reaction_id, reaction_id, reversible) bounds = match.group('bounds') lb, ub = _parse_bounds(bounds, reversible) objective = match.group('objective') obj = _parse_objective(objective) model.add_reaction(reaction, lb, ub, obj) if substrates: _parse_coefficients(substrates, model, reaction_id, sense=-1) if products: _parse_coefficients(products, model, reaction_id, sense=1) return reaction_id else: return None else: raise Exception('Unable to parse: ' + reaction_str)
def domain(self, post, domain): regex1 = r'^[Hh]ost\s*?:.*$' match = re.search(regex1, post, re.M) if not match: print colored("\nHost header not found in http request file.", 'white', 'on_red') print colored("\nExiting...", 'red') sys.exit(0) a = match.group() b = a.split(':')[1].lstrip() if b.find(domain) >= 0: return True else: return False
def domain(self,post,domain): regex1 = r'^[Hh]ost\s*?:.*$' match = re.search(regex1,post,re.M) if not match: print colored("\nHost header not found in http request file.",'white','on_red') print colored("\nExiting...",'red') sys.exit(0) a = match.group() b = a.split(':')[1].lstrip() if b.find(domain) >=0: return True else: return False
def countUrl(url, aName): # print('before start') tFile = open(aName, 'r') count = 0 pattern = '(GET|POST|PUT|DELETE) (.+) HTTP' for line in tFile: match = re.search(pattern, line) if match: mystr = match.group(2) index = mystr.find(url) if index > 0: count += 1 tFile.close() return count
def get_real_rc(output): """Parse out the final RC from MVS program output. Args: output (str): The MVS program output. Returns: int: The true program RC. """ true_rc = None match = search( r"HIGHEST\sRETURN\sCODE\sIS\s([0-9]+)", output, ) if match: true_rc = int(match.group(1)) return true_rc
def _lex_lines(self, lines): '''Helper method to lex lines of strings into Token stream''' lineno = 0 for lineno, line in enumerate(lines): for match in finditer(self.regex, line.rstrip()): if match is not None: for key in self.keys: lexeme = match.group(key) if ( lexeme is not None and lexeme.strip() != '' ): self.toks.append(Token(key, lexeme.strip(), lineno + 1, match.start())) break self.toks.append(Token('EOF', '__EOF__', lineno + 1, 0)) return self._coalesce()
def host_gcc_version(d): import re, subprocess compiler = d.getVar("BUILD_CC") try: env = os.environ.copy() env["PATH"] = d.getVar("PATH") output = subprocess.check_output("%s --version" % compiler, shell=True, env=env).decode("utf-8") except subprocess.CalledProcessError as e: bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) match = re.match(".* (\d\.\d)\.\d.*", output.split('\n')[0]) if not match: bb.fatal("Can't get compiler version from %s --version output" % compiler) version = match.group(1) return "-%s" % version if version in ("4.8", "4.9") else ""
def parse(self): self.cursor = 0 match = search(tags, self.html) if not match: return False s = 0 #start text e = 0 #end //it's inverted for text while True: s = match.end() + self.cursor self.anTag(match.group()[1:-1]) self.cursor += match.end() match = search(tags, self.html[self.cursor:]) if not match: break e = match.start() + self.cursor self.anText(self.html[s:e].strip())
def f_par(self, post, mode): post = post + '\r\n\r\n' post = re.sub(r'\r?\n\r?\n\r?\n\r?\n', r'\r\n\r\n', post, re.I | re.M | re.S) h_p = r'.*?(?=(\r?\n\r?\n))' d_p = r'((?<=(\n\n))|(?<=(\r\n\r\n))).*' hd = search(h_p, post, re.I | re.M | re.S) bd = search(d_p, post, re.I | re.M | re.S) b = hd.group() bd = bd.group().rstrip() hrs = [] regex = re.compile(r'^.*$', re.I | re.M) for match in regex.finditer(b): hrs.append(match.group()) f_l = hrs[0] del hrs[0] length = len(hrs) h_t = {} for count in range(0, length): sp = hrs[count].split(':', 1) h_t[sp[0]] = sp[1] method = f_l.split(' ', 1)[0] if 'Host' in h_t: url = h_t['Host'].lstrip().rstrip() + f_l.split()[1].lstrip() elif 'host' in h_t: url = h_t['host'].lstrip().rstrip() + f_l.split()[1].lstrip() else: print "Host header not found in http request file\nExiting..." sys.exit(0) mode = mode.lower() if mode == 'http': url = 'http://' + url url = url.replace(' ', '+') url = url.replace('#', r'%23') elif mode == 'https': url = 'https://' + url url = url.replace(' ', '+') url = url.replace('#', '%23') else: print "Please select mode between HTTP or HTTPS" return 0, 0, 0, 0 return h_t, bd, method, url
def f_par(self,post,mode): post = post + '\r\n\r\n' post = re.sub(r'\r?\n\r?\n\r?\n\r?\n',r'\r\n\r\n',post,re.I|re.M|re.S) h_p = r'.*?(?=(\r?\n\r?\n))' d_p = r'((?<=(\n\n))|(?<=(\r\n\r\n))).*' hd = search(h_p,post,re.I|re.M|re.S) bd = search(d_p,post,re.I|re.M|re.S) b=hd.group() bd = bd.group().rstrip() hrs = [] regex = re.compile(r'^.*$',re.I|re.M) for match in regex.finditer(b): hrs.append(match.group()) f_l = hrs[0] del hrs[0] length = len(hrs) h_t ={} for count in range(0,length): sp = hrs[count].split(':',1) h_t[sp[0]] = sp[1] method = f_l.split(' ',1)[0] if 'Host' in h_t: url = h_t['Host'].lstrip().rstrip()+f_l.split()[1].lstrip() elif 'host' in h_t: url = h_t['host'].lstrip().rstrip()+f_l.split()[1].lstrip() else: print "Host header not found in http request file\nExiting..." sys.exit(0) mode = mode.lower() if mode == 'http': url = 'http://'+ url url = url.replace(' ','+') url = url.replace('#',r'%23') elif mode =='https': url = 'https://'+ url url = url.replace(' ','+') url = url.replace('#','%23') else: print "Please select mode between HTTP or HTTPS" return 0,0,0,0 return h_t,bd,method,url
def host_gcc_version(d, taskcontextonly=False): import re, subprocess if taskcontextonly and d.getVar('BB_WORKERCONTEXT') != '1': return compiler = d.getVar("BUILD_CC") try: env = os.environ.copy() env["PATH"] = d.getVar("PATH") output = subprocess.check_output("%s --version" % compiler, \ shell=True, env=env, stderr=subprocess.STDOUT).decode("utf-8") except subprocess.CalledProcessError as e: bb.fatal("Error running %s --version: %s" % (compiler, e.output.decode("utf-8"))) match = re.match(".* (\d\.\d)\.\d.*", output.split('\n')[0]) if not match: bb.fatal("Can't get compiler version from %s --version output" % compiler) version = match.group(1) return "-%s" % version if version in ("4.8", "4.9") else ""
def get_package_version(pkg_name): def __get_env(): env = environ.copy() # Print output of commands in english env["LANG"] = "en_US.UTF-8" return env try: resp = run( split(f"apt-cache policy {pkg_name}"), check=False, capture_output=True, timeout=5, env=__get_env(), ) output = str(resp.stdout, "utf8") match = search("Installed: (.*)", output) return match.group(1) if match else "" except Exception as e: logger.warning(f"get_package_version {pkg_name} failed: {e}") return ""
def m(match): pair = (1, 2) if match.group(1) is not None else (3, 4) return match.group(pair[0])+match.group(pair[1])
def to_open_referral(entry): # Default values. city, state, zip, = '', '', '' languages = entry.languages emails = entry.emails short_description = entry.description[:100] phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') newphone = phonePattern.search(entry.phone) newphonebase = "" newext = "" if newphone: newphonebase = newphone.group(1) + newphone.group(2) + newphone.group(3) newext = newphone.group(4) newfax = phonePattern.search(entry.fax) newfaxbase = "" newfaxext = "" if newfax: newfaxbase = newfax.group(1) + newfax.group(2) + newfax.group(3) newfaxext = newfax.group(4) #name, title = entry.contact, '' # Apply fanciness. if ', San Francisco' in entry.address: entry.address = entry.address.replace(', San Francisco', '') city = 'San Francisco' if ', CA' in entry.address: entry.address = entry.address.replace(', CA', '') state = 'CA' zip_regex = '( [0-9]{5})$' match = re.search(zip_regex, entry.address) if match: zip = match.group(0).strip() entry.address = re.sub(zip_regex, '', entry.address) if len(entry.program_name.strip()) == 0: entry.program_name = entry.organization_name languages = EnglishListParser.parse_list(languages) emails = EnglishListParser.parse_list(emails) commapos = entry.name.find(',') fulllen = len(entry.name) if commapos > 0: contact_name = entry.name[0:commapos] contact_title = entry.name[commapos+1:fulllen].strip() else: contact_name = entry.name contact_title = 'NA' #if ',' in name: #name, title = [s.strip() for s in name.rsplit(',', 1)] # short_description = sent_detector.tokenize( # entry.services_provided.strip())[0] # Fill in the blanks. # Look here for field definitions: https://github.com/sfbrigade/ohana-api/wiki/Populating-the-Postgres-database-from-a-JSON-file#accessibility return { 'name':entry.organization_name, 'notes':"", 'locations':[ { 'name':entry.program_name, 'contacts_attributes':[ { 'name':contact_name, 'title':contact_title, #TODO - need to split out from name based on comma } ], 'description':entry.description, 'short_desc':short_description, 'address_attributes':{ 'street':entry.address , 'city':city , #TODO - need to grab out cities other than SF 'state':state , 'zip':zip }, "hours":entry.hours , "accessibility": [ entry.accessibility ], "languages": languages, "emails": emails, "faxes_attributes": [ { "number": newfaxbase } ], "phones_attributes": [ { "number": newphonebase, "extension": newext } ], "urls": [ entry.urls ], "services_attributes": [ { "name": entry.program_name, "description": entry.description, "audience": entry.audience, "eligibility": entry.eligibility, "fees": entry.fees, "how_to_apply": entry.how_to_apply, "keywords": [entry.keywords], #TODO - wrap items in double quotes. I think... } ], } ], }
pattern = r'spider[-,_ ]?man.' if re.match(pattern, dailybugle, re.IGNORECASE): print dailybugle ''' Match and capture group Match dates formatted like MM/DD/YYYY , MM-DD-YY, ... ''' date = '12/30/1969' regex = re.compile(r'^(\d\d)[-/](\d\d)[/-](\d\d(?:\d\d)?)$') match = regex.match(date) if match: month = match.group(1) print month day = match.group(2) print day year = match.group(3) print year ''' Simple substitution Convert <br> to </br> for XHTML compliance ''' text = 'Hello world. <br>' regex = re.compile(r'<br>',re.IGNORECASE) repl = r'<br />' result = regex.sub(repl, text)
def substitution(match): result = self.callback('sub_', name, match) if result is None: result = match.group(0) # The entire match return result
def sub_emphasis(self, match): #强调的内容 return '<em>%s</em>' % match.group(1)
def sub_url(self, match): #强调的内容 return '<a href="%s">%s</a>' % (match.group(1), match.group(1))
def sub_img(self, match): return '<img src="%s" alt="%s" />' % (match.group(1), match.group(1))
def sub_video(self, match): return '<video src="%s" controls="controls" width=600> 您的浏览器不支持 video 标签。 </video>' % match.group( 1)
def group_if_not_none(match: Any) -> Union[str, None]: return match.group() if match is not None else None
from re import match, search, findall, finditer, sub pattern = r"(?P<first>abc)def(ghi)j" m1 = match(pattern, "abcdefghij") if m1: print(m1.group(1)) print(m1.group(2)) print(m1.groups()) print(m1.group()) # # Example as name: import re pattern = r"(?P<first>gol)(?:dys)(ran)" match = re.match(pattern, "goldysran") if match: print(match.group("first")) print(match.groups()) # gol ('gol', 'ran') # # in simple words ?:.... skips the word but numbring remains same as you can see in the the given example(dys) skipped.... # What would be the result of len(match.groups()) of a match of (a)(b(?:c)(d)(?:e))? ---> 3 # Group 1 - (a) # Group 2 - (bd) # Group 3 - (d) # Length of Groups = 3 # The full code would be: import re pattern = r"(a)(b(?:c)(d)(?:e))"