Look at the definition of re.sub:
re.sub(pattern, repl, string[, count, flags])
The 4th argument is the count, you are using re.MULTILINE (which is 8) as the count, not as a flag.
Either use a named argument:
re.sub('^//', '', s, flags=re.MULTILINE)
Or compile the regex first:
re.sub(re.compile('^//', re.MULTILINE), '', s)
Answer from Moe on Stack OverflowPython documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
Deprecated since version 3.13: Passing count and flags as positional arguments is deprecated. In future Python versions they will be keyword-only parameters. re.subn(pattern, repl, string, count=0, flags=0)¶
GeeksforGeeks
geeksforgeeks.org › python › re-sub-python-regex
re.sub() - Python RegEx - GeeksforGeeks
July 23, 2025 - Python · import re a = "apple orange apple banana" pattern = "apple" repl = "grape" # Replace all occurrences of "apple" with "grape" result = re.sub(pattern, repl, a) print(result) Output · grape orange grape banana · Table of Content · Syntax of re.sub() Using Groups in re.sub() Limiting the Number of Replacements · re.sub(pattern, repl, string, count=0, flags=0) pattern: The regular expression pattern we want to match.
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python has a built-in package called re, which can be used to work with Regular Expressions. ... You can add flags to the pattern when using regular expressions.
LZone
lzone.de › examples › Python re.sub
Python re.sub Examples
import re result = re.sub(pattern, repl, string, count=0, flags=0); result = re.sub('abc', '', input) # Delete pattern abc result = re.sub('abc', 'def', input) # Replace pattern abc -> def result = re.sub(r'\s+', ' ', input) # Eliminate duplicate whitespaces using wildcards result = ...
Top answer 1 of 3
154
Look at the definition of re.sub:
re.sub(pattern, repl, string[, count, flags])
The 4th argument is the count, you are using re.MULTILINE (which is 8) as the count, not as a flag.
Either use a named argument:
re.sub('^//', '', s, flags=re.MULTILINE)
Or compile the regex first:
re.sub(re.compile('^//', re.MULTILINE), '', s)
2 of 3
12
re.sub('(?m)^//', '', s)
LearnByExample
learnbyexample.github.io › py_regular_expressions › flags.html
Flags - Understanding Python re(gex)?
This chapter showed some of the flags that can be used to change the default behavior of RE definition. And more special groupings were covered. 1) Remove from the first occurrence of hat to the last occurrence of it for the given input strings. Match these markers case insensitively. >>> s1 = 'But Cool THAT\nsee What okay\nwow quite' >>> s2 = 'it this hat is sliced HIT.' >>> pat = re.compile() ##### add your solution here >>> pat.sub('', s1) 'But Cool Te' >>> pat.sub('', s2) 'it this .'
PYnative
pynative.com › home › python › regex › python regex flags
Python Regex Flags (With Examples) – PYnative
April 13, 2021 - You will learn how to use all regex flags available in Python with short and clear examples.
Code.mu
code.mu › en › python › book › supreme › regular › string-flags
Flags for Python Regular Expression Strings
txt = 'aaa AAA bbb BBB' res = re.sub('[a-z]+', '!', txt, flags=re.IGNORECASE) print(res) ... But, if you need to replace all the characters, then putting a dot in the regular expression will not capture line breaks:
Johnlekberg
johnlekberg.com › blog › 2020-03-11-regex-flags.html
Using regular expression flags in Python
To turn a local flag off, write it like "(?-i:...)" instead of "(?i:...)". E.g. re.match("(?i)hello (?-i:there) dave", "HELLO THERE DAVE")
Python Examples
pythonexamples.org › python-re-sub
Python re.sub() - Replace using Regular Expression
import re pattern = '[0-9]+' string = 'Account Number - 12345, Amount - 586.32' repl = 'NN' print('Original string') print(string) result = re.sub(pattern, repl, string, count=2) print('After replacement') print(result) Account Number - 12345, Amount - 586.32 After replacement Account Number - NN, Amount - NN.32 · Only two pattern matching are replaced with the replacement string. Rest of the matchings are not replaced. In this example, we will pass optional flags argument re.IGNORECASE to re.sub() function.
Educative
educative.io › answers › what-is-the-resub-function-in-python
What is the re.sub() function in Python?
string: This denotes the string on which the re.sub() operation will be executed. count (Optional): This denotes the number of replacements that should occur. If we want to replace all matches, we can skip this parameter or set it to 0. flags (Optional): This serves to modify the behavior of the regular expression operation. For example, the value could be re.IGNORECASE or re.NOFLAG.
Codecademy
codecademy.com › docs › python › regular expressions › re.sub()
Python | Regular Expressions | re.sub() | Codecademy
October 14, 2024 - Master Python while learning data structures, algorithms, and more! ... Get a taste of regular expressions (regex), a powerful search pattern language to quickly find the text you're looking for. ... <count>: An integer specifying the number of occurrences to replace. The default is to replace all matches. <flags>: Specifies additional options such as IGNORECASE, VERBOSE, DOTALL, etc. The following example replaces all occurrences of “BI” with “business intelligence”:
Xah Lee
xahlee.info › python › python_regex_flags.html
Python: Regex Flags
〔see Python: Regex Syntax〕 · This flag changes the regex syntax, to allow you to add annotations in regex. Whitespace within the pattern is ignored, except when in a character class or preceded by an unescaped backslash, and, when a line contains a # neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored. # -*- coding: utf-8 -*- import re # example of the regex re.VERBOSE flag # matching a decimal number p1 = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X) p2 = re.compile(r"\d+\.\d*") # pattern p2 is same as p1 r1 = re.findall(p1, u"a3.45") r2 = re.findall(p2, u"a3.45") print r1[0].encode("utf8") # 3.45 print r2[0].encode("utf8") # 3.45
Python Tutorial
pythontutorial.net › home › python regex › python regex flags
Python Regex Flags
December 9, 2021 - In this example, the word 作法 was excluded from the resulting list. The Python regex flags are instances of the RegexFlag enumeration class. The regex flags change the way the regex engine performs pattern matching. The regex functions like match, fullmatch, findall, finditer, search, split, sub, subn accept a flags parameter that can be a flag a combination of regex flags.
Python Tutorial
pythontutorial.net › home › python regex › python regex sub()
Python Regex sub() Function
December 10, 2021 - import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'<b>\1<\\b>' html = re.sub(pattern, replacement, s) print(html)Code language: Python (python) ... In this example, the pattern r'\*(.*?)\*' find the text that begins and ends with the asterisk (*).
LearnByExample
learnbyexample.github.io › python-regex-cheatsheet
Python regular expression cheatsheet and examples
By default, Python maintains a small list of recently used RE, so the speed benefit doesn't apply for trivial use cases. >>> pet = re.compile(r'dog') >>> type(pet) <class 're.Pattern'> >>> bool(pet.search('They bought a dog')) True >>> bool(pet.search('A cat crossed their path')) False >>> pat = re.compile(r'\([^)]*\)') >>> pat.sub('', 'a+b(addition) - foo() + c%d(#modulo)') 'a+b - foo + c%d' >>> pat.sub('', 'Hi there(greeting).
PYnative
pynative.com › home › python › regex › python regex replace pattern in a string using re.sub()
Python Regex Replace Pattern in a string using re.sub()
July 19, 2021 - .By default, the count is set to zero, which means the re.sub() method will replace all pattern occurrences in the target string. flags: Finally, the last argument is optional and refers to regex flags. By default, no flags are applied. There are many flag values we can use. For example, the re.I is used for performing case-insensitive searching and replacing.