-3

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

starwarrior8809
  • 361
  • 1
  • 10

2 Answers2

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