[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

for line3 in myips matching too longer matches.

On 26Jun2019 14:21, Chris Roberts <thecjguy1 at> wrote:
>           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?)
>line1 is
>line2 is              - address:  # myhost
>I think the problem is here:
>line1.rstrip(?\n?) in line2.strip(?\n?):
>I want it to
>match only AND
>NOT match[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:

  '' in ' - address:  # myhost'

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

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

it does a membership test i.e. does the _exact string_ '' 
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 


    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.

Cameron Simpson <cs at>