On 26Jun2019 14:21, Chris Roberts <thecjg...@gmail.com> wrote:

###
CODE:
          elif line1.rstrip(‘\n’) in line2.strip(‘\n’):
              for line3 in myips:
                  print “###”
                  print “line1 is %s” % line1.rstrip(‘\n’)
                  print “line2 is %s” % line2.strip(‘\n’)
###
OUTPUT:
line1 is 10.10.168.2
line2 is              - address: 10.10.168.27  # myhost
###

I think the problem is here:
line1.rstrip(‘\n’) in line2.strip(‘\n’):

I want it to
match only 10.10.168.2 AND 10.10.168.2:
NOT match 10.10.168.2[0-9]

If someone out there knows a simple solution. I would love to see it.

Well the usual solution is to break line2 in to "words".

When you go:

 '10.10.168.2' in ' - address: 10.10.168.27  # myhost'

you're testing a substring match. But if you go:

 '10.10.168.2' in ['-', 'address:', '10.10.168.27', '#', 'myhost']

it does a membership test i.e. does the _exact string_ '10.10.168.2' occur in this list of strings.

(This isn't magic, the "in" operator uses an object's __contains__ method (or falls back to iteration and comparison), so the nature of the test is driven by the type of the object being tested (on the right, for "in")).

So...

   address = line1.rstrip('\n')
   words = line2.split()
   if address in words:
       ...

Personally I'd be being pickier about line1 as well, but ifyour input is well defined that may not matter.

Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to