Matt Norris
Matt Norris

Reputation: 8836

How do I get matched characters from Python regex?

I am trying to match a data URI pattern of data:\w+/\w+;\w+,\w+ in Python. How do I retain each match to use later (essentially, each of the w+ pieces)?

Upvotes: 1

Views: 1507

Answers (4)

Francisc
Francisc

Reputation: 80415

Put them in groups. I don't know Python, but this is the same in every language. You group things in RegEx using brackets.

data:(\w+)/(\w+);(\w+),(\w+)

Then, using Python RegExp functions, get the groups that match.

Upvotes: 0

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

Capture them into groups, i.e. data:(\w+)/(\w+);(\w+),(\w+). You can then use m.group(x) or m.groups() (where m is a match object) to obtain them (note that group 0 is the entire matched string).

Upvotes: 7

jpm
jpm

Reputation: 3155

You need groups. From the Python Documentation

(...) Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use ( or ), or enclose them inside a character class: [(] [)].

Upvotes: 2

tlunter
tlunter

Reputation: 724

I believe it would be r'data:(\w+)/(\w+);(\w+),(\w+)' or something beyond that.

Upvotes: 0

Related Questions