I have a a previously matched pattern such as:
<a href="somelink here something">
Now I wish to extract only the value of a specific attribute(s) in the tag such but this may be anything an occur anywhere in the tag.
regex_pattern=re.compile('href=\"(.*?)\"')
Now I can use the above to match the attribute and the value part but I need to extract only the (.*?) part. (Value)
I can ofcourse strip href=" and " later but I’m sure I can use regex properly to extract only the required part.
In simple words I want to match
abcdef=\"______________________\"
in the pattern but want only the
____________________
Part
How do I do this?
Take a look at the
.group()method on regular expressionMatchObjectresults.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.MatchObjectare returned by severalrefunctions and methods, including the.search()and.finditer()functions.Demonstration:
From the Regular Expression syntax documentation on the
(...)parenthesis syntax: