相思资源网 Design By www.200059.com
本文主要关于python的正则表达式的符号与方法。
findall: 找寻所有匹配,返回所有组合的列表
search: 找寻第一个匹配并返回
sub: 替换符合规律的内容,并返回替换后的内容
.:匹配除了换行符以外的任意字符
a = 'xy123'
b = re.findall('x...',a)
print(b)
# ['xy12']
*:匹配前一个字符0次或者无限次
a = 'xyxy123'
b = re.findall('x*',a)
print(b)
# ['x', '', 'x', '', '', '', '', '']
"htmlcode">
a = 'xy123'
b = re.findall('x"htmlcode">
b = re.findall('xx.*xx',secret_code)
print(b)
# ['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']
.*"htmlcode">
c = re.findall('xx.*"htmlcode">
d = re.findall('xx(.*"htmlcode">
s = '''sdfxxhello
xxfsdfxxworldxxasdf'''
d = re.findall('xx(.*"htmlcode">
s2 = 'asdfxxIxx123xxlovexxdfd'
f = re.search('xx(.*"htmlcode">
s = '123rrrrr123'
output = re.sub('123(.*"htmlcode">
import re
def multiply(m):
# Convert group 0 to an integer.
v = m.group(0)
print(v)
# Multiply integer by 2.
# ... Convert back into string and return it.
print('basic/'+v)
return 'basic/'+v
结果如下
>autoencoder.png
basic/autoencoder.png
RNN.png
basic/RNN.png
rnn_step_forward.png
basic/rnn_step_forward.png
rnns.png
basic/rnns.png
rnn_cell_backprop.png
basic/rnn_cell_backprop.png
LSTM.png
basic/LSTM.png
LSTM_rnn.png
basic/LSTM_rnn.png
attn_mechanism.png
basic/attn_mechanism.png
attn_model.png
basic/attn_model.png
仿照上面案例,我们可以方便的对我们的任务进行定制。
subn相比sub,subn返回元组,第二个元素表示替换发生的次数:
import re
def add(m):
# Convert.
v = int(m.group(0))
# Add 2.
return str(v + 1)
# Call re.subn.
result = re.subn("\d+", add, "1 2 3 4 5")
print("Result string:", result[0])
print("Number of substitutions:", result[1])
>
Result string: 11 21 31 41 51
Number of substitutions: 5
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无python中正则表达式的使用方法的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。