Reputation: 1
I am new to python and I am not familiar with regex pattern. I am using re package to get particular text in my code. but it doesn't seems to work. please help!
import re
text = '<pre><a href="1.sh">1.sh'
filename = re.match(r'\D+="[*]"\D', text)
print(text)
print(filename)
output:
<pre><a href="1.sh">1.sh
None
I am expecting the filename '1.sh', it can be either the text within double quote or the text after '>'
1.sh also in my scenario, the filename varies, it may be filename.txt or number.ps1 or number.shUpvotes: 0
Views: 37
Reputation: 6234
import re
text = '<pre><a href="1.sh">1.sh'
filename = re.search(r'(?<=href=")[^"]+', text).group()
print(text)
print(filename)
Output:
<pre><a href="1.sh">1.sh
1.sh
Upvotes: 1
Reputation: 1112
Try this
import re
text='<pre><a href="1.sh">1.sh'
filename=re.sub('^<[ ]*a[ ]+.*href[ ]*=[ ]*', '', re.sub('.*>$', '', text).strip('"')
Upvotes: 0