Z
Z
zasqer2016-03-26 05:00:02
Python
zasqer, 2016-03-26 05:00:02

How to remove square brackets and content using regular expressions?

there is a line from which you need to remove the square brackets and their contents, if inside the brackets there is an integer
I try like this:

import re
s = "one [two] three [1] [2]"
print(re.sub(r'/\[.*?\]/', '', s))

I get the same string as output:
"one [two] three [1] [2]"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zasqer, 2016-03-26
@zasqer

re.sub('[\d]', '', s)
For the 128th time, I promise myself to learn regular expressions. And every time I think - "Yes, no, no longer needed!"

A
abcd0x00, 2016-03-26
@abcd0x00

>>> import re
>>> 
>>> s = "one [two] three [1] [2]"
>>> 
>>> re.sub(r'\[\d+\]', '', s)
'one [two] three  '
>>>

S
SirHardon, 2021-01-31
@SirHardon

s = "one [two] three [1] [2]"
print(re.sub('\[|\]', '', s))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question