İşte benim kod:Python regex arama
#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r'.*IgnoreR.*', f.read())
print(strings)
veri döndürür
ama belli Normal ifade eşlemeyi gerekir: Ben kodunu değiştirmek durumunda
^\s*[^#]*IgnoreRhosts\s+yes
: örneğin basitçe için:
strings = re.search(r'^IgnoreR.*', f.read())
hatta
strings = re.search(r'^.*IgnoreR.*', f.read())
Geri bir şey alamıyorum. Bu modda olmadan her zaman \n
tarafından ^
takabilmek
#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r"^\s*[^#]*IgnoreRhosts\s+yes", f.read(), flags=re.MULTILINE)
print(strings.group(0))
Not: Ben
Neden "re.search (r"^\ s * [^ #] * IgnoreRhosts \ s + yes ", f.read())' yi kullanmıyorsunuz? –
Python'un regex sözdizimi, perl'in – SethMMorton
ile aynıdır.^Ign çalışmıyorsa, r '^ \ s * [^ #] * IgnoreRhosts \ s + yes' işlevini kullanamazsınız. En düşük ortak paydayı arıyordum. –