Extract Portions Of Text If Regex In Python
Solution 1:
Just use re.search('href=\"(.*?)\"', yourtext).group(1)
on the matched string yourtext
and it will yield the matched group.
Solution 2:
Take a look at the .group()
method on regular expression MatchObject
results.
Your regular expression has an explicit group match group (the part in ()
parethesis), and the .group()
method gives you direct access to the string that was matched within that group. MatchObject
are returned by several re
functions and methods, including the .search()
and .finditer()
functions.
Demonstration:
>>>import re>>>example = '<a href="somelink here something">'>>>regex_pattern=re.compile('href=\"(.*?)\"') >>>regex_pattern.search(example)
<_sre.SRE_Match object at 0x1098a2b70>
>>>regex_pattern.search(example).group(1)
'somelink here something'
From the Regular Expression syntax documentation on the (...)
parenthesis syntax:
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: [(] [)].
Post a Comment for "Extract Portions Of Text If Regex In Python"