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 Overflow
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
4 days ago - Perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made). The expression’s behaviour can be modified by specifying a flags value.
🌐
PYnative
pynative.com › home › python › regex › python regex flags
Python Regex Flags (With Examples) – PYnative
April 13, 2021 - Python regex allows optional flags to specify when using regular expression patterns with match(), search(), and split(), among others.
🌐
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.
🌐
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.
🌐
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 .'
🌐
Code.mu
code.mu › en › python › book › supreme › regular › string-flags
Flags for Python Regular Expression Strings
txt = '''aaa bbb''' res = re.sub('.', '!', txt) print(res) ... You can also pass several flags in the parameter by placing the operator + between them.
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular Expression HOWTO — Python 3.14.3 documentation
The pattern may be provided as an object or as a string; if you need to specify regular expression flags, you must either use a pattern object as the first parameter, or use embedded modifiers in the ...
Find elsewhere
🌐
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")
🌐
Medium
medium.com › @wepypixel › complete-python-regex-replace-guide-using-re-sub-pypixel-9b30b2604d7a
Complete Python Regex Replace Guide using re.sub() | PyPixel | by Stilest | Medium
December 8, 2023 - Here, pattern, replacement, and string are mandatory to be provided while count and flags are default to 0 and are optional. Let’s discuss what these 5 are one by one: pattern (required) : This specifies the regular expression pattern to search for in the input string. Raw strings like r"pattern" are recommended. replacement (required) : The replacement substring that matched pattern text gets substituted with.
🌐
Codecademy
codecademy.com › docs › python › regular expressions › re.sub()
Python | Regular Expressions | re.sub() | Codecademy
October 14, 2024 - Replace matching substrings with a new string for all occurrences, or a specified number.
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
For the above you could write the ... brackets). The re.sub(pat, replacement, str) function searches for all the instances of pattern in the given string, and replaces them....
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex flags
Python Regex Flags
December 9, 2021 - 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.
🌐
Educative
educative.io › answers › what-is-the-resub-function-in-python
What is the re.sub() function in Python?
string: This denotes the string ... can skip this parameter or set it to 0. flags (Optional): This serves to modify the behavior of the regular expression operation....
🌐
Real Python
realpython.com › regex-python-part-2
Regular Expressions: Regexes in Python (Part 2) – Real Python
June 16, 2023 - In the previous tutorial in this series, you covered a lot of ground. You saw how to use re.search() to perform pattern matching with regexes in Python and learned about the many regex metacharacters and parsing flags that you can use to fine-tune your pattern-matching capabilities.
🌐
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 = re.sub('abc(def)ghi', r'\1', input) # Replace a string with a part of itself ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-re-compile
Python - re.compile() - GeeksforGeeks
July 23, 2025 - In this code we are using flags (Modifiers that change the behavior of the regex.) Python ·
🌐
Finxter
blog.finxter.com › home › learn python blog › python regex flags
Python Regex Flags - Be on the Right Side of Change
November 18, 2020 - In many Python regex functions, you see a third argument flags. What are they and how do they work? Flags allow you to control the regular expression engine. Because regular expressions are so powerful, they are a useful way of switching on and off certain features (e.g. whether to ignore ...