1

I need to fix some text that lies between delimiters for the following cases:
Case 1: {12345} (numbers between curlies) should become item_12345 (added 'item_', removed braces).
Case 2: [999] (numbers between square brackets) should become total_999

So this string: {242424} from X [100] bulks, linked to {57575757} from Y for [500] units should appear like this: item_242424 from X total_100 bulks, linked to item_57575757 from Y for total_500 units

How can this be done with regex?

user1102018
  • 4,369
  • 6
  • 26
  • 33
  • 4
    What have you come up with so far? – tuxuday May 09 '12 at 11:32
  • Hi, pretty much nothing. I don't know how to separate the text from the tags and how to define multiple patterns with multiple replacements. This is a complete black hole for me and I wasn't able to find any answer – user1102018 May 09 '12 at 11:36
  • If you had spent even couple of hours, assuming you are new to programming/regex, should have something to share here. – tuxuday May 09 '12 at 11:45
  • Well, following the answer below, I guess my error was placing the parentheses outside the curlies. This gave me an undesirable result. – user1102018 May 09 '12 at 11:52

2 Answers2

4

This should get you started:

s = '{123} and [456]'

s = re.sub(r'\{(.+?)\}', r'foo_\1', s)
s = re.sub(r'\[(.+?)\]', r'bar_\1', s)

print s
georg
  • 211,518
  • 52
  • 313
  • 390
1
>>> import re
>>> curly = re.compile('\{([0-9]+)\}')
>>> square = re.compile('\[([0-9]+)\]')
>>> s = "{242424} from X [100] bulks, linked to {57575757} from Y for [500]"
>>> re.sub(square, lambda x: 'total_'+x.group(1), re.sub(curly, lambda x: 'item_
'+x.group(1),s))
'item_242424 from X total_100 bulks, linked to item_57575757 from Y for total_50
0'
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284