-2

Text-

.1. This is just awesome.2. Google just ruined Apple.3. Apple ruined itself! pattern = (dot)(number)(dot)(singlespace)

Imagine you have 30 to 40 sentences with paragraph numbers in the above pattern. A <p> tag should be replaced behind THE PARAGRAPH NUMBER! USING re.sub()

I want the text to be:

</p> <p style="text-align: justify;">1. This is just awesome.</p> <p style="text-align: justify;">2. Google just ruined Apple.</p> <p style="text-align: justify;">3. Apple ruined itself!
nhahtdh
  • 52,949
  • 15
  • 113
  • 149
  • possible duplicate of this: http://stackoverflow.com/questions/5984633/python-re-sub-group-number-after-number – Mariy Feb 14 '12 at 15:01

1 Answers1

2

This is what matching groups are for in regular expresssions.

What you want is something like this:

new_string = re.sub(r'\.(\d+\. )', '</p><p style="text-align: justify;">\\1', old_string)
Ian Clelland
  • 39,551
  • 8
  • 79
  • 83