-1

How can I get a string between 2 specified strings in Python3 using regex?

b'SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.11\r\n'

desired output:

>>> SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.11
# ipsort.py
import re

f = open("ssh.txt", "r")

line = f.readline()

for line in f:
    version = re.search(r"b'(.*?)\r\n'", line)
    new_file = open("ssh_versions.txt", "a")
    new_file.write(version)
    new_file.close()

Traceback (most recent call last):
  File "ipsort.py", line 11, in <module>
    new_file.write(version)
TypeError: write() argument must be str, not None
j86h
  • 45
  • 1
  • 8

1 Answers1

1

You need to double the \ in the regexp, because otherwise it's matching CR and LF characters rather than literal \r and \n.

You also need to use version.group(1) to get the string that the capture group matched.

version = re.search(r"b'(.*?)\\r\\n'", line)
if version:
    with open("ssh_versions.txt", "a") as new_file:
        new_file.write(version.group(1))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you, it works. It was seriously frustrating. Regular expressions are complicated, its like learning a new language. – j86h May 01 '20 at 17:15