newdev14
newdev14

Reputation: 1111

regex - capture group

I trying to write a regex to match the following at the beginning of a new line
- a number followed by parantheses e.g. 2) or 8)
- a number followed by period e.g. 5 - the character '-' - the character '*'

the following strings should match

I have tried this but it doesn't quite get me what I am looking for.

re.findall(r'(?m)\s*^[-*(\d.)(\d\))]',item)

Upvotes: 0

Views: 47

Answers (2)

Martin Konecny
Martin Konecny

Reputation: 59701

Try

re.findall(r'^\s*(\d+(\)|\.)|-|\*)', item, re.MULTILINE)

It will match all sequences of numbers followed by a closing parenthesis or period as well as dashes and stars at the beginning of the line.

Example: https://regex101.com/r/cR2lZ5/6

Upvotes: 1

Dribbler
Dribbler

Reputation: 4701

Assuming that your quote marks " are not included, and that each line is a separate string,

^\d\.|^\d\)|^\-|^\*

Would be the regular expression. | is OR, \d is a digit, and you escape the special characters ".", ")", "-", and "*" by putting a backslash in front of them.

You can test your regular expressions here. Good luck!

Upvotes: 0

Related Questions