Python re.subn() Examples
The following are 30 code examples for showing how to use re.subn(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
re
, or try the search function
.
Example 1
Project: drydock Author: airshipit File: resolver.py License: Apache License 2.0 | 6 votes |
def resolve_reference_ucp(cls, design_uri): """Retrieve artifacts from a Airship service endpoint. Return a byte array of the response content. Assumes Keystone authentication required. :param design_uri: Tuple as returned by urllib.parse for the design reference """ ks_sess = KeystoneUtils.get_session() (new_scheme, foo) = re.subn(r'^[^+]+\+', '', design_uri.scheme) url = urllib.parse.urlunparse( (new_scheme, design_uri.netloc, design_uri.path, design_uri.params, design_uri.query, design_uri.fragment)) LOG.debug("Calling Keystone session for url %s" % str(url)) resp = ks_sess.get(url, timeout=get_client_timeouts()) if resp.status_code >= 400: raise errors.InvalidDesignReference( "Received error code for reference %s: %s - %s" % (url, str(resp.status_code), resp.text)) return resp.content
Example 2
Project: GitGot Author: BishopFox File: gitgot.py License: GNU Lesser General Public License v3.0 | 6 votes |
def regex_search(checks, repo): output = "" for line in repo.decoded_content.splitlines(): for check in checks: try: line = line.decode('utf-8') except AttributeError: pass try: (line, inst) = re.subn( check, bcolors.BOLD + bcolors.OKBLUE + r'\1' + bcolors.ENDC, line) if inst > 0: output += "\t" + line + "\n" print("\t", line) break except Exception as e: print( bcolors.FAIL + "ERROR: ", e, bcolors.ENDC, bcolors.WARNING, "\nCHECK: ", check, bcolors.ENDC, "\nLINE: ", line) print(bcolors.HEADER + "End of Matches" + bcolors.ENDC) return output
Example 3
Project: tudouNLP Author: fennuDetudou File: convert_to_tagdata.py License: MIT License | 6 votes |
def _parse_text(text: list): bises = [] for line in text: # remove POS tag line, _ = re.subn('\\n', '', line) if line == '' or line == '\n': continue words = re.split('\s+', line) if len(words) > MAX_LEN_SIZE: texts = re.split('[。?!,.?!,]/w', line) if len(min(texts, key=len)) > MAX_LEN_SIZE: continue bises.extend(_parse_text(texts)) else: bises.append(_tag(words)) return bises
Example 4
Project: conan-center-index Author: conan-io File: conanfile.py License: MIT License | 6 votes |
def package(self): self.copy("LICENSE", dst="licenses", src=self._source_subfolder) if self.settings.compiler == "Visual Studio": cmake = self._configure_cmake() cmake.install() else: autotools = self._configure_autotools() autotools.install() os.unlink(os.path.join(self.package_folder, "lib", "libapr-1.la")) tools.rmdir(os.path.join(self.package_folder, "build-1")) tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) apr_rules_mk = os.path.join(self.package_folder, "bin", "build-1", "apr_rules.mk") apr_rules_cnt = open(apr_rules_mk).read() for key in ("apr_builddir", "apr_builders", "top_builddir"): apr_rules_cnt, nb = re.subn("^{}=[^\n]*\n".format(key), "{}=$(_APR_BUILDDIR)\n".format(key), apr_rules_cnt, flags=re.MULTILINE) if nb == 0: raise ConanException("Could not find/replace {} in {}".format(key, apr_rules_mk)) open(apr_rules_mk, "w").write(apr_rules_cnt)
Example 5
Project: vhdl-style-guide Author: jeremiah-c-leary File: utils.py License: GNU General Public License v3.0 | 6 votes |
def change_word(oLine, sWord, sNewWord, iMax=1): ''' Changes one word in the line to another. Parameters: oLine: (line object) sWord: (string) sNewWord: (string) ''' sLine = oLine.line tLine = re.subn(r'\b' + re.escape(sWord) + r'\b', sNewWord, sLine, iMax) sLine = tLine[0] if tLine[1] == 0: tLine = re.subn(' ' + sWord + ';', sNewWord + ';', sLine, iMax) sLine = tLine[0] if tLine[1] == 0: tLine = re.subn(sWord + '$', sNewWord, sLine, iMax) sLine = tLine[0] sLine = tLine[0] oLine.update_line(sLine)
Example 6
Project: ironpython2 Author: IronLanguages File: test_re.py License: Apache License 2.0 | 6 votes |
def test_none(self): for x in 'compile search match split findall finditer'.split(): y = getattr(re, x) self.assertRaises(TypeError, y, None) self.assertRaises(TypeError, y, None, None) self.assertRaises(TypeError, y, None, 'abc') self.assertRaises(TypeError, y, 'abc', None) # Other exceptional input tests for x in (re.sub, re.subn): self.assertRaises(TypeError, x, 'abc', None, 'abc') self.assertRaises(TypeError, x, 'abc', None, None) self.assertRaises(TypeError, x, None, 'abc', 'abc') self.assertRaises(TypeError, x, 'abc', 'abc', None) self.assertRaises(TypeError, re.escape, None)
Example 7
Project: ironpython2 Author: IronLanguages File: test_re.py License: Apache License 2.0 | 6 votes |
def test_subn(self): # subn tup = re.subn("ab", "cd", "abababababab") self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ab", "cd", "abababababab", 0) self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ab", "cd", "abababababab", 1) self.assertTrue(tup == ('cdababababab', 1)) tup = re.subn("ab", "cd", "abababababab", 10) self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ababab", "cd", "ab", 10) self.assertTrue(tup == ('ab', 0)) tup = re.subn("ababab", "cd", "ab") self.assertTrue(tup == ('ab', 0)) tup = re.subn("(ab)*", "cd", "abababababab", 10) self.assertTrue(tup == ('cd', 1)) tup = re.subn("(ab)?", "cd", "abababababab", 10) self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
Example 8
Project: chainer-compiler Author: pfnet-research File: rebase_elichika_type_tests.py License: MIT License | 6 votes |
def rebase_testcase(filename, model_name, gen_model_fn): model, forward_args = gen_model_fn() id2type, id2node = type_inference_tools.generate_type_inference_results( model, forward_args, is_debug=False) sio = six.StringIO() type_inference_tools.generate_assertion("id2type", id2type, id2node, sio) with open(filename) as f: code = f.read() begin_marker = '# === BEGIN ASSERTIONS for {} ==='.format(model_name) end_marker = '# === END ASSERTIONS for {} ==='.format(model_name) regexp = begin_marker + '.*?' + end_marker new_assertions = begin_marker + '\n' + sio.getvalue() + ' ' * 8 + end_marker code, num_replaced = re.subn(regexp, new_assertions, code, count=1, flags=re.DOTALL | re.MULTILINE) if not num_replaced: raise RuntimeError('No assertion markers for {}'.format(model_name)) with open(filename, 'w') as f: f.write(code)
Example 9
Project: SalesforceXyTools Author: exiahuang File: baseutil.py License: Apache License 2.0 | 6 votes |
def del_comment(soql): result = soql if soql: # TODO # soql = soql.strip().replace('\t', ' ').replace('\r\n', ' ').replace('\n', ' ') soql = soql.strip().replace('\t', ' ') # delete // comment result1, number = re.subn("//.*", "", soql) # delete /**/ comment result, number = re.subn("/\*([\s|\S]*?)\*/", "", result1, flags=re.M) result = result.strip() # show_in_panel(result) return result # get sobject name from soql
Example 10
Project: CDTB Author: CommunityDragon File: hashes.py License: GNU Lesser General Public License v3.0 | 6 votes |
def substitute_skin_numbers(self): """Replace skinNN, multiple combinaisons""" characters = {} # {char: ({skin}, {(format, N})} regex = re.compile(r'/characters/([^/]+)/skins/(base|skin\d+)/') for p in self.known.values(): m = regex.search(p) if not m: continue char, skin = m.groups() if m.group(1) == 'sightward': continue c = characters.setdefault(char, (set(), set())) c[0].add(skin) c[1].add(re.subn(r'(?:base|skin\d+)', '%s', p)) # generate all combinations logger.debug(f"substitute skin numbers: {len(characters)} characters") for char, (skins, formats) in progress_iterator(characters.items(), lambda v: v[0]): for fmt, nocc in formats: self.check_iter(fmt % p for p in itertools.combinations(skins, nocc))
Example 11
Project: armada Author: airshipit File: document.py License: Apache License 2.0 | 6 votes |
def resolve_reference_ucp(cls, design_uri): """Retrieve artifacts from a Airship service endpoint. Return a byte array of the response content. Assumes Keystone authentication required. :param design_uri: Tuple as returned by urllib.parse for the design reference """ ks_sess = ks_utils.get_keystone_session() (new_scheme, foo) = re.subn(r'^[^+]+\+', '', design_uri.scheme) url = urllib.parse.urlunparse( ( new_scheme, design_uri.netloc, design_uri.path, design_uri.params, design_uri.query, design_uri.fragment)) LOG.debug("Calling Keystone session for url %s" % str(url)) resp = ks_sess.get(url) if resp.status_code >= 400: raise InvalidPathException( "Received error code for reference %s: %s - %s" % (url, str(resp.status_code), resp.text)) return resp.content
Example 12
Project: armada Author: att-comdev File: document.py License: Apache License 2.0 | 6 votes |
def resolve_reference_ucp(cls, design_uri): """Retrieve artifacts from a UCP service endpoint. Return a byte array of the response content. Assumes Keystone authentication required. :param design_uri: Tuple as returned by urllib.parse for the design reference """ ks_sess = KeystoneUtils.get_session() (new_scheme, foo) = re.subn('^[^+]+\+', '', design_uri.scheme) url = urllib.parse.urlunparse( (new_scheme, design_uri.netloc, design_uri.path, design_uri.params, design_uri.query, design_uri.fragment)) LOG.debug("Calling Keystone session for url %s" % str(url)) resp = ks_sess.get(url) if resp.status_code >= 400: raise InvalidPathException( "Received error code for reference %s: %s - %s" % (url, str(resp.status_code), resp.text)) return resp.content
Example 13
Project: ironpython3 Author: IronLanguages File: test_re.py License: Apache License 2.0 | 6 votes |
def test_none(self): for x in 'compile search match split findall finditer'.split(): y = getattr(re, x) self.assertRaises(TypeError, y, None) self.assertRaises(TypeError, y, None, None) self.assertRaises(TypeError, y, None, 'abc') self.assertRaises(TypeError, y, 'abc', None) # Other exceptional input tests for x in (re.sub, re.subn): self.assertRaises(TypeError, x, 'abc', None, 'abc') self.assertRaises(TypeError, x, 'abc', None, None) self.assertRaises(TypeError, x, None, 'abc', 'abc') self.assertRaises(TypeError, x, 'abc', 'abc', None) self.assertRaises(TypeError, re.escape, None)
Example 14
Project: ironpython3 Author: IronLanguages File: test_re.py License: Apache License 2.0 | 6 votes |
def test_subn(self): # subn tup = re.subn("ab", "cd", "abababababab") self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ab", "cd", "abababababab", 0) self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ab", "cd", "abababababab", 1) self.assertTrue(tup == ('cdababababab', 1)) tup = re.subn("ab", "cd", "abababababab", 10) self.assertTrue(tup == ('cdcdcdcdcdcd', 6)) tup = re.subn("ababab", "cd", "ab", 10) self.assertTrue(tup == ('ab', 0)) tup = re.subn("ababab", "cd", "ab") self.assertTrue(tup == ('ab', 0)) tup = re.subn("(ab)*", "cd", "abababababab", 10) self.assertTrue(tup == ('cd', 1)) tup = re.subn("(ab)?", "cd", "abababababab", 10) self.assertTrue(tup == ('cdcdcdcdcdcd', 6))
Example 15
Project: pracmln Author: danielnyga File: mrf.py License: BSD 2-Clause "Simplified" License | 6 votes |
def _materialize_weights(self, verbose=False): ''' materialize all formula weights. ''' max_weight = 0 for f in self.formulas: if f.weight is not None and f.weight != HARD: w = str(f.weight) variables = re.findall(r'\$\w+', w) for var in variables: try: w, numReplacements = re.subn(r'\%s' % var, self.mln.vars[var], w) except: raise Exception("Error substituting variable references in '%s'\n" % w) if numReplacements == 0: raise Exception("Undefined variable(s) referenced in '%s'" % w) w = re.sub(r'domSize\((.*?)\)', r'self.domsize("\1")', w) try: f.weight = float(eval(w)) except: sys.stderr.write("Evaluation error while trying to compute '%s'\n" % w) raise max_weight = max(abs(f.weight), max_weight)
Example 16
Project: pracmln Author: danielnyga File: mrf.py License: BSD 2-Clause "Simplified" License | 6 votes |
def _materialize_weights(self, verbose=False): ''' materialize all formula weights. ''' max_weight = 0 for f in self.formulas: if f.weight is not None and f.weight != HARD: w = str(f.weight) variables = re.findall(r'\$\w+', w) for var in variables: try: w, numReplacements = re.subn(r'\%s' % var, self.mln.vars[var], w) except: raise Exception("Error substituting variable references in '%s'\n" % w) if numReplacements == 0: raise Exception("Undefined variable(s) referenced in '%s'" % w) w = re.sub(r'domSize\((.*?)\)', r'self.domsize("\1")', w) try: f.weight = float(eval(w)) except: sys.stderr.write("Evaluation error while trying to compute '%s'\n" % w) raise max_weight = max(abs(f.weight), max_weight)
Example 17
Project: bi-lstm-crf Author: GlassyWing File: ner_data_preprocess.py License: Apache License 2.0 | 6 votes |
def _parse_text(text: list): bises = [] for line in text: # remove POS tag line, _ = re.subn('\\n', '', line) if line == '' or line == '\n': continue words = re.split('\s+', line) if len(words) > MAX_LEN_SIZE: texts = re.split('[。?!,.?!,]/w', line) if len(min(texts, key=len)) > MAX_LEN_SIZE: continue bises.extend(_parse_text(texts)) else: bises.append(_tag(words)) return bises
Example 18
Project: ChatterBot Author: gunthercox File: parsing.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def datetime_parsing(text, base_date=datetime.now()): """ Extract datetime objects from a string of text. """ matches = [] found_array = [] # Find the position in the string for expression, function in regex: for match in expression.finditer(text): matches.append((match.group(), function(match, base_date), match.span())) # Wrap the matched text with TAG element to prevent nested selections for match, value, spans in matches: subn = re.subn( '(?!<TAG[^>]*?>)' + match + '(?![^<]*?</TAG>)', '<TAG>' + match + '</TAG>', text ) text = subn[0] is_substituted = subn[1] if is_substituted != 0: found_array.append((match, value, spans)) # To preserve order of the match, sort based on the start position return sorted(found_array, key=lambda match: match and match[2][0])
Example 19
Project: rdopkg Author: softwarefactory-project File: specfile.py License: Apache License 2.0 | 6 votes |
def _create_new_magic_comment(self, name, value): # check to see if we have any magic comments in right slot # after SourceX and before Patch Y - if so insert at begining block # otherwise insert a new block as before if re.findall(self.RE_IN_MAGIC_COMMENTS, self._txt, flags=re.M): self._txt = re.sub( self.RE_IN_MAGIC_COMMENTS, r'\g<1># %s=%s\n\g<2>' % (name, value), self.txt, count=1, flags=re.M) return self._txt, n = re.subn( self.RE_PATCH, r'\n#\n# %s=%s\n#\n\g<1>' % (name, value), self.txt, count=1, flags=re.M) if n != 1: self._txt, n = re.subn( self.RE_AFTER_SOURCES, r'\g<1>#\n# %s=%s\n#\n\n' % (name, value), self.txt, count=1, flags=re.M) if n != 1: raise exception.SpecFileParseError( spec_fn=self.fn, error="Unable to create new #%s magic comment." % name)
Example 20
Project: grlc Author: CLARIAH File: gquery.py License: MIT License | 5 votes |
def count_query_results(query, endpoint): """ Returns the total number of results that query 'query' will generate WARNING: This is too expensive just for providing a number of result pages Providing a dummy count for now """ # number_results_query, repl = re.subn("SELECT.*FROM", "SELECT COUNT (*) FROM", query) # if not repl: # number_results_query = re.sub("SELECT.*{", "SELECT COUNT(*) {", query) # number_results_query = re.sub("GROUP\s+BY\s+[\?\_\(\)a-zA-Z0-9]+", "", number_results_query) # number_results_query = re.sub("ORDER\s+BY\s+[\?\_\(\)a-zA-Z0-9]+", "", number_results_query) # number_results_query = re.sub("LIMIT\s+[0-9]+", "", number_results_query) # number_results_query = re.sub("OFFSET\s+[0-9]+", "", number_results_query) # # glogger.debug("Query for result count: " + number_results_query) # # # Preapre HTTP request # headers = { 'Accept' : 'application/json' } # data = { 'query' : number_results_query } # count_json = requests.get(endpoint, params=data, headers=headers).json() # count = int(count_json['results']['bindings'][0]['callret-0']['value']) # glogger.info("Paginated query has {} results in total".format(count)) # # return count return 1000
Example 21
Project: SublimeKSP Author: nojanath File: ksp_plugin.py License: GNU General Public License v3.0 | 5 votes |
def on_load(self, view): if self.is_probably_ksp_file(view): s = codecs.open(view.file_name(), 'r', 'latin-1').read() mixed_line_endings = re.search(r'\r(?!\n)', s) and '\r\n' in s if mixed_line_endings: s, changes = re.subn(r'\r+\n', '\n', s) # normalize line endings if changes: s = '\n'.join(x.rstrip() for x in s.split('\n')) # strip trailing white-space too while we're at it view.run_command('replace_text_with', {'new_text': s}) sublime.set_timeout(lambda: sublime.status_message('EOL characters automatically fixed. Please save to keep the changes.'), 100) self.set_ksp_syntax(view)
Example 22
Project: GitGot Author: BishopFox File: gitgot.py License: GNU Lesser General Public License v3.0 | 5 votes |
def regex_validator(args, state): with open(args.checks, "r") as fd: for line in fd.read().splitlines(): if line.startswith("#") or len(line) == 0: continue try: re.subn(line, r'\1', "Expression test") except sre_constants.error as e: print(bcolors.FAIL + "Invalid Regular expression:\n\t" + line) if "group" in str(e): print( "Ensure expression contains" "a capture group for matches:\n\t" + str(e)) sys.exit(-1) state.checks.append(line) split = [] if not (state.query[0] == "\"" and state.query[-1] == "\""): split = re.split(GITHUB_WHITESPACE, state.query) for part in [state.query] + split: if part: escaped_query = re.escape(part) if split else \ part.replace("\"", "") state.checks.append("(?i)(" + escaped_query + ")") return state
Example 23
Project: jawfish Author: war-and-code File: test_re.py License: MIT License | 5 votes |
def test_re_subn(self): self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2)) self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1)) self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0)) self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4)) self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))
Example 24
Project: jawfish Author: war-and-code File: test_re.py License: MIT License | 5 votes |
def test_bug_13899(self): # Issue #13899: re pattern r"[\A]" should work like "A" but matches # nothing. Ditto B and Z. self.assertEqual(re.findall(r'[\A\B\b\C\Z]', 'AB\bCZ'), ['A', 'B', '\b', 'C', 'Z']) # FIXME: brython: implement test.support # @bigmemtest(size=_2G, memuse=1) # def test_large_search(self, size): # # Issue #10182: indices were 32-bit-truncated. # s = 'a' * size # m = re.search('$', s) # self.assertIsNotNone(m) # self.assertEqual(m.start(), size) # self.assertEqual(m.end(), size) # FIXME: brython: implement test.support # The huge memuse is because of re.sub() using a list and a join() # to create the replacement result. # @bigmemtest(size=_2G, memuse=16 + 2) # def test_large_subn(self, size): # # Issue #10182: indices were 32-bit-truncated. # s = 'a' * size # r, n = re.subn('', '', s) # self.assertEqual(r, s) # self.assertEqual(n, size + 1)
Example 25
Project: conan-center-index Author: conan-io File: conanfile.py License: MIT License | 5 votes |
def package(self): self.copy("COPYING*", src=self._source_subfolder, dst="licenses") with self._build_context(): autotools = self._configure_autotools() autotools.install() tools.rmdir(os.path.join(self._datarootdir, "info")) tools.rmdir(os.path.join(self._datarootdir, "man")) os.unlink(os.path.join(self.package_folder, "lib", "libltdl.la")) if self.options.shared: self._rm_binlib_files_containing(self._static_ext, self._shared_ext) else: self._rm_binlib_files_containing(self._shared_ext) import re files = ( os.path.join(self.package_folder, "bin", "libtool"), os.path.join(self.package_folder, "bin", "libtoolize"), ) replaces = { "GREP": "/usr/bin/env grep", "EGREP": "/usr/bin/env grep -E", "FGREP": "/usr/bin/env grep -F", "SED": "/usr/bin/env sed", } for file in files: contents = open(file).read() for key, repl in replaces.items(): contents, nb1 = re.subn("^{}=\"[^\"]*\"".format(key), "{}=\"{}\"".format(key, repl), contents, flags=re.MULTILINE) contents, nb2 = re.subn("^: \\$\\{{{}=\"[^$\"]*\"\\}}".format(key), ": ${{{}=\"{}\"}}".format(key, repl), contents, flags=re.MULTILINE) if nb1 + nb2 == 0: raise ConanException("Failed to find {} in {}".format(key, repl)) open(file, "w").write(contents) binpath = os.path.join(self.package_folder, "bin") if self.settings.os == "Windows": os.rename(os.path.join(binpath, "libtoolize"), os.path.join(binpath, "libtoolize.exe")) os.rename(os.path.join(binpath, "libtool"), os.path.join(binpath, "libtool.exe"))
Example 26
Project: recruit Author: Frank-qlu File: from_template.py License: Apache License 2.0 | 5 votes |
def find_and_remove_repl_patterns(astr): names = find_repl_patterns(astr) astr = re.subn(named_re, '', astr)[0] return astr, names
Example 27
Project: pybids Author: bids-standard File: inflect.py License: MIT License | 5 votes |
def present_participle(self, word): """ Return the present participle for word. word is the 3rd person singular verb. """ plv = self.plural_verb(word, 2) for pat, repl in ( (r"ie$", r"y"), (r"ue$", r"u"), # TODO: isn't ue$ -> u encompassed in the following rule? (r"([auy])e$", r"\g<1>"), (r"ski$", r"ski"), (r"[^b]i$", r""), (r"^(are|were)$", r"be"), (r"^(had)$", r"hav"), (r"^(hoe)$", r"\g<1>"), (r"([^e])e$", r"\g<1>"), (r"er$", r"er"), (r"([^aeiou][aeiouy]([bdgmnprst]))$", r"\g<1>\g<2>"), ): (ans, num) = re.subn(pat, repl, plv) if num: return "%sing" % ans return "%sing" % ans # NUMERICAL INFLECTIONS
Example 28
Project: sacred Author: IDSIA File: commands.py License: MIT License | 5 votes |
def help_for_command(command): """Get the help text (signature + docstring) for a command (function).""" help_text = pydoc.text.document(command) # remove backspaces return re.subn(".\\x08", "", help_text)[0]
Example 29
Project: lambda-packs Author: ryfeus File: from_template.py License: MIT License | 5 votes |
def find_and_remove_repl_patterns(astr): names = find_repl_patterns(astr) astr = re.subn(named_re, '', astr)[0] return astr, names
Example 30
Project: ironpython2 Author: IronLanguages File: test_re.py License: Apache License 2.0 | 5 votes |
def test_re_subn(self): self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2)) self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1)) self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0)) self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4)) self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))