Reputation: 119
I have a pattern that works fine on regexr.com with pcre but when I use it with python it doesn't match anything. the pattern is:
.*(?<=RSA SHA256:).*(?:.*\n){3}.*
and it matches the data on the website but when I run this on my python script it doesn't. The goal is to match Accepted publickey and the next 3 lines. Thank you!
script below:
import re
Accepted_publickey=r'.*(?<=RSA SHA256:).*(?:.*\n){3}.*'
file=open('secure')
for items in file:
re1=re.search(Accepted_publickey,items)
if re1:
print(re1.group())
The actual data is:
Oct 21 17:27:21 localhost sshd[19772]: Accepted publickey for vagrant from 192.168.2.140 port 54614 ssh2: RSA SHA256:uDsE4ecSD9ElWQ5Q0fdMsbqEzOe0Hszilv8xhU6dT6M
Oct 21 17:27:22 localhost sshd[19772]: pam_unix(sshd:session): session opened for user vagrant by (uid=0)
Oct 21 17:27:22 localhost sshd[19772]: User child is on pid 19774
Oct 21 17:27:22 localhost sshd[19774]: Starting session: shell on pts/2 for vagrant from 192.168.2.140 port 54614 id 0
Upvotes: 2
Views: 2151
Reputation: 163207
You don't have to use a lookbehind, you could match the value.
To match the 3 following lines, you could switch the newline and .*
to omit the last .*
^.*\bRSA SHA256:.*(?:\n.*){3}
^
Start of string.*\bRSA SHA256:.*
Match RSA SHA256:
in the string preceded by a word boundary(?:\n.*){3}
repeat 3 times a newline followed by matching any char except a newline 3 timesIn your code you might use read():
import re
Accepted_publickey = r'^.*RSA SHA256:.*(?:.*\n){3}.*'
f = open('secure')
items = f.read()
re1 = re.search(Accepted_publickey, items, re.M)
if re1:
print(re1.group())
Upvotes: 2