[英]Python regex: replace ignoring empty string
我試圖使用re
在Python中用正則表達式替換給定的模式。 這是我編寫的Python代碼:
import re
fname = './prec.f90'
f = open(fname)
lines = f.readlines()
f.close()
for i, line in enumerate(lines):
search = re.findall('([\d*]?\.[\d*]?)+?[^dq\_]', line)
if search != []:
print('Real found in line #%d: ' %i)
print search
print('The following line:\n %s' %line)
print('will be replace by:')
newline = re.sub('([\d*]?\.[\d*]?)+?[^dq\_]', r'\g<1>d0\g<2>', line)
print('%s' %newline)
而且prec.f90
包含類似的內容(這只是一個示例,並不意味着我要替換的所有字符串都具有[x]_[yz] = ...;
):
x_pr = 0.1; y_pr = 0.2; z_pr = 0.1q0
x_sp = 0.1; y_sp = 0.1d0; z_sp = 0.1q0
x_dp = 0.1; y_dp = 0.1d0; z_dp = 0.1q0
x_qp = .1; y_qp = 0.1d0; z_qp = 0.1q0
x_db = 0.; y_db = 0.1d0; y_db = 0.1q0
我的目標是修改所有模式,例如0.1
, .1
和0.
,以獲得類似0.1d0
; 我不想修改其他模式。 問題是re.findall('[\\d*]?\\.[\\d*]?)+?([^dq\\_]')
匹配我正在尋找的模式,但也返回了一個空字符串其他的。 因此,當我運行這段代碼時,它失敗了,無法為空字符串替換匹配re.sub()
中的第一組和第二組。
我猜一種解決方案是忽略re.sub
空字符串,或者在其中添加類似條件參數的內容,但是我不知道如何做。
任何幫助,將不勝感激!
您可以將sub
簡化為
>>> str="x_db = 0.; y_db = 0.1d0; y_db = 0.1q"
>>> re.sub(r'(0\.1|\.1|0\.)(?=;)', r'\g<1>0d0', str)
'x_db = 0.0d0; y_db = 0.1d0; y_db = 0.1q'
正則表達式(0\\.1|\\.1|0\\.)(?=;)
將匹配0.1
, .1
和0.
;
(x_[a-zA-Z]{2}\s*=)\s+[^;]+
嘗試一下。替換為\\1 0.1d0
。請參見演示。
http://regex101.com/r/qZ6sE3/2
import re
p = re.compile(ur'(x_[a-zA-Z]{2}\s*=)\s+[^;]+')
test_str = u"x_pr = 0.1; y_pr = 0.2; z_pr = 0.1q0\nx_sp = 0.1; y_sp = 0.1d0; z_sp = 0.1q0\nx_dp = 0.1; y_dp = 0.1d0; z_dp = 0.1q0\nx_qp = .1; y_qp = 0.1d0; z_qp = 0.1q0\nx_db = 0.; y_db = 0.1d0; y_db = 0.1q0"
subst = u"\1 0.1d0"
result = re.sub(p, subst, test_str)
我終於想出了按預期工作的這段代碼:
import re
fname = './prec.f90'
f = open(fname)
lines = f.readlines()
f.close()
# If there was no end of the line character (\n) we would need to check if
# this is the end of the line (something like ([^dq\_0-9]|$)
regex = re.compile(r'(\d*\.\d*)([^dq\_0-9])')
for i, line in enumerate(lines):
search = regex.findall(line)
if search != []:
print('Real found in line #%d: ' %i)
print search
print('The following line:\n %s' %line)
print('will be replace by:')
newline = regex.sub(r'\g<1>d0\g<2>', line)
print('%s' %newline)
我首先想出了更復雜的正則表達式([\\d*]?\\.[\\d*]?)+?[^dq\\_]
因為否則我總是匹配以d
, q
或_
。 這似乎是由於\\d*
不夠貪心。 在“忽略”集中添加0-9
可解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.