I'm working on the text processing and I need to remove all the tuples from the text, tuples can have arbitrary number of elements (e.g. () or (1,2,3)
) ,but the elements will always be integers. Can somebody help me to write regex for this, I'm really new to regular expressions and I have no idea how to do it!
Kind Regards,
Dre
Asked
Active
Viewed 239 times
-3

starwarrior8809
- 361
- 1
- 10
-
1What you have tried so far ? – Sahil Gulati Sep 27 '17 at 10:14
-
Did you try solving it yourself first? – Ahmed Dhanani Sep 27 '17 at 10:20
-
There are plenty of online resources to try regexes and fettle them until they do what you need. OS is not a *"write my code for me"* service. – SiHa Sep 27 '17 at 10:23
-
`\((?:\d+,)*?\d*?\)` – kaza Sep 27 '17 at 10:26
2 Answers
0
So you want something like this:
a = 'This is (1,2,3) just () a test.'
re.sub('\([0-9,]*\)','',a)
#'This is just a test.'
Note: if this is the case you'll be left with some extra whitespace.
To deal with that you can use:
re.sub('\s+',' ',a)
#'This is just a test.'

zipa
- 27,316
- 6
- 40
- 58
0
You can try this:
import re
text = '(4, 5, 2), (23, 13, 100), (43, 567, 1)'
data = re.findall("(?<=\().*?(?=\))", text)
final_data = [tuple(map(int, tuple(re.split(",\s*", i)))) for i in data]
Output:
[(4, 5, 2), (23, 13, 100), (43, 567, 1)]

Ajax1234
- 69,937
- 8
- 61
- 102