0

Just learning RegEx in Python, and got stuck on this case:

str = '''
Here are some order bullets:
i. Foo
ii. Bar
iii. Baz
iv. YOLO
...
x. LOL
'''

How do I get to this:

str = '''
Here are some order bullets:
- i. Foo
- ii. Bar
- iii. Baz
- iv. YOLO
...
- x. LOL
'''
1
  • Look into re.sub. Commented Dec 4, 2017 at 14:44

1 Answer 1

3

You can use this regex in MULTILINE mode:

>>> print re.sub(r'(?m)^([a-zA-Z0-9]+\.)', r'- \1', str)

Here are some order bullets:
- i. Foo
- ii. Bar
- iii. Baz
- iv. YOLO
...
- x. LOL

RegEx Breakup:

Search:

  • (?m): Enable MULTILINE mode
  • ^: Start
  • ([a-zA-Z0-9]+\.): Match and group 1+ alphanumeric characters

Replacement:

  • -: Place literal "- "
  • \1: Back-reference to captured group #1
Sign up to request clarification or add additional context in comments.

2 Comments

Wow that was fast! Thx so much
Thank you so much for the help and the nice explanation - I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.