简体   繁体   中英

python regex to get particular word

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.sh
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

Try this

import re
text='<pre><a href="1.sh">1.sh'
filename=re.sub('^<[ ]*a[ ]+.*href[ ]*=[ ]*',  '', re.sub('.*>$', '', text).strip('"')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM