はここに私のコードです:Pythonの正規表現検索は
#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r'.*IgnoreR.*', f.read())
print(strings)
データを返す
が、私は、特定の正規表現のマッチングが必要です。私は自分のコードを変更した場合
^\s*[^#]*IgnoreRhosts\s+yes
:例えば単に:
strings = re.search(r'^IgnoreR.*', f.read())
あるいは
strings = re.search(r'^.*IgnoreR.*', f.read())
私は戻って何も得られません。このモードずに常に\n
で^
を置き換えることができ
#!/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))
注:私は
なぜre.search(r "^ \ s * [^#] * IgnoreRhosts \ s + yes"、f.read()) 'を使用していないのですか? –
Pythonの正規表現の構文は、perlの – SethMMorton
と同じです。^ Ignが機能しない場合、r '^ \ s * [^#] * IgnoreRhosts \ s + yes'という方法はありません。私は最低の共通点を求めていました。 –