0

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

1 Answer 1

1

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)']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alecxe, how could i return the string "(Group)" as well. Like this.['IT_PC (Group)', 'HR_PC (Group)', 'Finance_PC (Group)', 'Testing_PC (Group)']
Yes, it helps. Thanks for your prompt reply ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.