nooper
nooper

Reputation: 701

Python regular expression replace string

I have the following code:

 a="32<2>fdssa</2>ffdsa32"
 re.sub(r'<(\d+)>|</(\d+)>',"item",a)

The result I get:

32itemfdssaitemffdsa32

I want the result:

32<item>fdssa</item>ffdsa32

Upvotes: 1

Views: 70

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174844

You need to capture </ part.

re.sub(r'(</?)\d+>',r"\1item>",a)

Since I made / as optional, (</?) will capture < or </

Example:

>>> a="32<2>fdssa</2>ffdsa32"
>>> re.sub(r'(</?)\d+>',r"\1item>",a)
'32<item>fdssa</item>ffdsa32'

Upvotes: 3

nu11p01n73R
nu11p01n73R

Reputation: 26677

>>> re.sub(r'(</?)\d+(?=>)', r'\1item', a)
'32<item>fdssa</item>ffdsa32'
  • (</?) matches < or </ captures to \1

  • \d+ matches one or more digits.

  • (?=>) positive look ahead, checks if the digits is followed by >, but doesn't consume them

Upvotes: 1

Related Questions