kafter2
kafter2

Reputation: 2253

Regular Expression return the word before a string - python

I am writing a python script and would like to match all Group object name from a large file, an example of the raw data as below:

IT_PC (Group) -Host: 192.168.103.144 -Host: 192.168.103.145 -Network: 192.168.103.0 255.255.255.0 HR_PC (Group) -Host: 192.168.65.145 -Host: 192.168.62.146 -Host: 192.168.62.154
Finance_PC (Group) -Finance_PC_192.168.41.125
Testing_PC (Group) -Host: 192.168.129.1 -Host: 192.168.129.97 -Host: 192.168.59.81 -Host: 192.168.59.82

My required output shall be like this:

IT_PC (Group)
HR_PC (Group)
Finance_PC (Group)
Testing_PC (Group)

I am trying to use below regular express to match my required result but it only return the first one ['IT_PC (Group)']. Is there any advice for me thanks.

source = "IT_PC (Group) -Host: 192.168.103.144 -Host: 192.168.103.145 -Network: 192.168.103.0 255.255.255.0 HR_PC (Group) -Host: 192.168.65.145 -Host: 192.168.62.146 -Host: 192.168.62.154 Finance_PC (Group) -Finance_PC_192.168.41.125 Testing_PC (Group) -Host: 192.168.129.1 -Host: 192.168.129.97 -Host: 192.168.59.81 -Host: 192.168.59.82"

data = ".*? (?= \(group\))"
a = re.findall(data, source)
print a

Upvotes: 0

Views: 2395

Answers (1)

alecxe
alecxe

Reputation: 474221

I'd use the \w+(?= \(Group\)) expression which would match one or more alphanumeric characters (A-Za-z0-9_) followed by a space and a (Group):

>>> re.findall(r"\w+(?= \(Group\))", source)
['IT_PC', 'HR_PC', 'Finance_PC', 'Testing_PC']

Or, you can be even more specific about the group object name format and require one or more upper case letters after the underscore:

>>> re.findall(r"\w+_[A-Z]+(?= \(Group\))", source)
['IT_PC', 'HR_PC', 'Finance_PC', 'Testing_PC']

Or, if you need Group as well:

>>> re.findall(r"\w+ \(Group\)", source)
['IT_PC (Group)', 'HR_PC (Group)', 'Finance_PC (Group)', 'Testing_PC (Group)']
>>> re.findall(r"\w+_[A-Z]+ \(Group\)", source)
['IT_PC (Group)', 'HR_PC (Group)', 'Finance_PC (Group)', 'Testing_PC (Group)']

Upvotes: 1

Related Questions