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
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.
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)
Videos
11:09
Python Regular Expressions Tutorial #9: Compilation Flags - YouTube
13:51
Python Regex Flags [Ultimate Guide] - YouTube
09:37
Python standard library: Regular expression flags (re.I, re.S, re ...
14:41
RegEx in Python (Part-11) | Compilation Flags - YouTube
Python Regular Expressions -part #10 - Flags
08:19
Python [re] 03 Flags - YouTube
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.
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.
Top answer 1 of 3
147
Yes, but you have to OR them together:
x = re.findall(pattern=r'CAT.+?END', string='Cat \n eND', flags=re.I | re.DOTALL)
2 of 3
22
You can't put the flags within a tuple. Use the pipe character (OR operand) within your flags:
x = re.findall(r'CAT.+?END','Cat \n eND',flags=re.I | re.DOTALL)
GeeksforGeeks
geeksforgeeks.org › python › python-flags-to-tune-the-behavior-of-regular-expressions
Python Flags to Tune the Behavior of Regular Expressions - GeeksforGeeks
October 1, 2020 - Python offers some flags to modify the behavior of regular expression engines.
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....
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 ...