stereoscopique
stereoscopique

Reputation: 15

Python Regex find all matches after specific word

I have a string as below "Server: myserver.mysite.com\r\nAddress: 111.122.133.144\r\n\r\nName: myserver.mysite.com\r\nAddress: 123.144.412.111\r\nAliases: alias1.myserver.mysite.com\r\n\t myserver.mysite.com\r\n\r\n"

I'm currently struggling to write a function in python that will find all aliases and put them in a list. So basically, I need a list that will be ['alias1.myserver.mysite.com', 'myserver.mysite.com']

I tried the following code

pattern = '(?<=Aliases:  )([\S*]+)'
name =  re.findall(pattern, mystring)

but it only matches the first alias and not both of them.

Any ideas on this?

Greatly appreciated!

Upvotes: 0

Views: 86

Answers (2)

Booboo
Booboo

Reputation: 44013

Try the following:

import re

s = "Server:  myserver.mysite.com\r\nAddress:  111.122.133.144\r\n\r\nName:    myserver.mysite.com\r\nAddress:  123.144.412.111\r\nAliases:  alias1.myserver.mysite.com\r\n\t  myserver.mysite.com\r\n\r\n"

l = re.findall(r'\S+', s.split('Aliases:  ')[1])
print(l)

Prints:

['alias1.myserver.mysite.com', 'myserver.mysite.com']

Explanation

First we split the string into two pieces and keep the second piece with s.split('Aliases: ')[1]. This evaluates to the part of the string that follows 'Aliases: '.

Next we use findall with the regaular expression:

\S+

This matches all consecutive strings of one or more non-space characters.

But this can be more simply done in this case without using a regex:

s = "Server:  myserver.mysite.com\r\nAddress:  111.122.133.144\r\n\r\nName:    myserver.mysite.com\r\nAddress:  123.144.412.111\r\nAliases:  alias1.myserver.mysite.com\r\n\t  myserver.mysite.com\r\n\r\n"

l = s.split('Aliases:  ')[1].split()
print(l)

Upvotes: 1

Yash Makan
Yash Makan

Reputation: 764

Try this :

import re
regex = re.compile(r'[\n\r\t]')
t="Server:  myserver.mysite.com\r\nAddress:  111.122.133.144\r\n\r\nName:    myserver.mysite.com\r\nAddress:  123.144.412.111\r\nAliases:  alias1.myserver.mysite.com\r\n\t  myserver.mysite.com\r\n\r\n"
t = regex.sub(" ", t)
t = t.split("Aliases:")[1].strip().split()
print(t)

Upvotes: 0

Related Questions