Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re.sub on lists - python 3

I have a list on which I try to remove special chars using a loop. When I've tried to remove those special chars without a loop, it worked. But with a loop didn't work, but did run (and I don't know how). Those special chars are :"[" and "]". It's probably something very simple or with list's comprehension, which I tried some but didn't quite work ( How do you use a regex in a list comprehension in Python? )

Could you help? I'm new to Python, but it would help a lot. Please share your knowledge!

Output should be : [ '1', '2' ]

My code:

import re
# Case 1 : Sub with no loop
w = '[ 1,2,3,4 ]'

outer= re.compile("\[(.+)\]")
m = outer.search(w)
inner_str = m.group(1)

# Case 2 - Sub with loop
x = [ '[1]', '[2]' ]

for item in x:
    if item == re.match('\[(.+)\]', item):
        print(re.sub("\[(.+)\]", "", item))
like image 703
svacx Avatar asked Apr 10 '15 21:04

svacx


People also ask

WHAT IS RE sub in python3?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string.

Does re sub replace all occurrences?

sub() Replace matching substrings with a new string for all occurrences, or a specified number.

What does re SUB do RegEx?

The re. sub() function is used to replace occurrences of a particular sub-string with another sub-string. This function takes as input the following: The sub-string to replace.

How do you use re sub?

If you want to replace a string that matches a regular expression (regex) instead of perfect match, use the sub() of the re module. In re. sub() , specify a regex pattern in the first argument, a new string in the second, and a string to be processed in the third.


2 Answers

You can do this using a list comprehension, you mean something like this?

>>> import re
>>> x = [ '[1]', '[2]' ]
>>> [re.sub(r'\W', '', i) for i in x]
['1', '2']

The token \W matches any non-word character.

like image 175
hwnd Avatar answered Sep 25 '22 01:09

hwnd


Assuming you are trying to keep the stuff inside the brackets, this works:

import re
# Case 1 : no sub!
w = '[ 1,2,3,4 ]'

outer= re.compile("\[(.+)\]")
m = outer.search(w)
inner_str = m.group(1)
print(inner_str)

# Case 2 - no sub!
x = [ '[1]', '[2]' ]

y = []
for item in x:
    match = outer.match(item)
    if match:
        y.append(match.group(1))

print(y)
like image 25
tdelaney Avatar answered Sep 27 '22 01:09

tdelaney