Python Forum
Matching multiple parts in string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Matching multiple parts in string (/thread-37044.html)

Pages: 1 2 3 4


RE: Matching multiple parts in string - bowlofred - May-06-2022

Would need to know more about it. That snippet doesn't run by itself and I don't know what a and b are. Could you show a small working example?


RE: Matching multiple parts in string - fozz - May-06-2022

Oke, here is the working code, it is triggered when someone joins a irc channel with a host/ip added in the userlist.txt:
# -*- coding: utf-8 -*-
userlist = '/home/whatever/.weechat/userlist.txt'
users = []
def op_join_cb(data, signal, signal_data): #raw server stuff when someone joins the irc channel ([email protected])
    global users 
    args = signal_data.split(' ')[0:3] #JOIN
    server = signal.split(",")[0] #network name resolve
    msg = weechat.info_get_hashtable("irc_message_parse", {"message": signal_data}) #some irc conversation stuff
    buffer = weechat.info_get("irc_buffer", "%s,%s" % (server, msg["channel"])) #weechat irc client buffer/channel select where the host is joining
    nick = args[0][1:].split('!')[0] #nickname of the joiner
    host = args[0][1:].split('!')[1].split('@')[1] #result: host.com, without nick!ident@ of the joiner on the channel
    chan = args[2][1:] #the channel on irc where the listed host joins
    if any(host in u for u in users): #this is the part that doesnt match a wildcarded host in the .txt file but a full match does it
            weechat.command(buffer, '/wait 1 /mode ' + chan + ' +o ' + nick) #weechat irc client command, give +@ (channel moderator) someone if host is in the list (or kick etc.)
    return weechat.WEECHAT_RC_OK #reference to the weechat irc client its oke and make progress
weechat.hook_signal('*,irc_in2_join', 'op_join_cb', '') #hooksignal for weechat irc client 
The userlist.txt is added with hosts (for example) like:
*!*@ABC.123.456.def
But I want to match joining users like:
*!*@*.123.456.def
or
*!*ident@ABC.*.456.def
or
*!*@ABC.123.456.*
Since not all hostnames/ip addresses are static and some parts are dynamic, thats why I want to wildcard some parts in the hosts/ip's.
The script I'm using is only matching/working when the full host is added in the userlist.txt like:
*!*@ABC.123.456.def
The first wildcards, exclamation mark and @ aren't a problem because the script can read the host in the userlist.txt file, If wildcards are added in the rest of the host (after the @) then it does'nt match anymore, what is happening? Is there a solution for this in phyton? Sorry, I'm really lost on this.


RE: Matching multiple parts in string - fozz - Jun-04-2022

(May-06-2022, 11:01 PM)fozz Wrote: Oke, here is the working code, it is triggered when someone joins a irc channel with a host/ip added in the userlist.txt:
# -*- coding: utf-8 -*-
userlist = '/home/whatever/.weechat/userlist.txt'
users = []
def op_join_cb(data, signal, signal_data): #raw server stuff when someone joins the irc channel ([email protected])
    global users 
    args = signal_data.split(' ')[0:3] #JOIN
    server = signal.split(",")[0] #network name resolve
    msg = weechat.info_get_hashtable("irc_message_parse", {"message": signal_data}) #some irc conversation stuff
    buffer = weechat.info_get("irc_buffer", "%s,%s" % (server, msg["channel"])) #weechat irc client buffer/channel select where the host is joining
    nick = args[0][1:].split('!')[0] #nickname of the joiner
    host = args[0][1:].split('!')[1].split('@')[1] #result: host.com, without nick!ident@ of the joiner on the channel
    chan = args[2][1:] #the channel on irc where the listed host joins
    if any(host in u for u in users): #this is the part that doesnt match a wildcarded host in the .txt file but a full match does it
            weechat.command(buffer, '/wait 1 /mode ' + chan + ' +o ' + nick) #weechat irc client command, give +@ (channel moderator) someone if host is in the list (or kick etc.)
    return weechat.WEECHAT_RC_OK #reference to the weechat irc client its oke and make progress
weechat.hook_signal('*,irc_in2_join', 'op_join_cb', '') #hooksignal for weechat irc client 
The userlist.txt is added with hosts (for example) like:
*!*@ABC.123.456.def
But I want to match joining users like:
*!*@*.123.456.def
or
*!*ident@ABC.*.456.def
or
*!*@ABC.123.456.*
Since not all hostnames/ip addresses are static and some parts are dynamic, thats why I want to wildcard some parts in the hosts/ip's.
The script I'm using is only matching/working when the full host is added in the userlist.txt like:
*!*@ABC.123.456.def
The first wildcards, exclamation mark and @ aren't a problem because the script can read the host in the userlist.txt file, If wildcards are added in the rest of the host (after the @) then it does'nt match anymore, what is happening? Is there a solution for this in phyton? Sorry, I'm really lost on this.
Any help on this one please?


RE: Matching multiple parts in string - Gribouillis - Jun-04-2022

Have you tried using standard module fnmatch?
>>> from fnmatch import fnmatch
>>> fnmatch("123.456.987", "*.456.*")
True



RE: Matching multiple parts in string - fozz - Jun-07-2022

Thank you Gribouillis,
I added it in the code like this:
from fnmatch import fnmatch
if fnmatch(host, users):
  do something
But i get the next error message:
TypeError: expected str, bytes or os.PathLike object, not list
What am I doing wrong, how do I implement fnmatch in this script to read the wildcarded hosts from the userlist.txt file?


RE: Matching multiple parts in string - DeaD_EyE - Jun-07-2022

(Jun-07-2022, 12:09 PM)fozz Wrote: What am I doing wrong, how do I implement fnmatch in this script to read the wildcarded hosts from the userlist.txt file?

The error message is right. The function needs one str and one str which is the pattern/wildcard.
But your code has a list with strings.

import fnmatch


wildcarded = '*.456.789.*.11.12.*'
values = [
    '123.456.789.10.11.12.abc',
    '123.456.789.10.11.12.eee',
    '123.666.789.10.11.12.abc',
]

for value in values:
    if fnmatch.fnmatch(value, wildcarded):
        print(value)
If you want to collect the resulting values in a list, then you've to create a list and append each element, which matches the wildcard, to the list.

import fnmatch


wildcarded = '*.456.789.*.11.12.*'
values = [
    '123.456.789.10.11.12.abc',
    '123.456.789.10.11.12.eee',
    '123.666.789.10.11.12.abc',
]

results = []
for value in values:
    if fnmatch.fnmatch(value, wildcarded):
        results.append(value)



print(results)



RE: Matching multiple parts in string - fozz - Jun-07-2022

Thank you DeaD_EyE,

I still can't get this to work, the situation is like this:
I have a userlist.txt with wildcarded hosts added like this:

*!*@*.contaboserver.net
*!*ident@dynamic.*.com
*!*@webchat.*.afg.net
*!*[email protected].*

etc. etc.

When a user joins my irc channel with a host like:
[email protected] or
[email protected] or
[email protected] or
[email protected]
python must look in in the userlist.txt and if the host from the joining user on the irc channel matches a wildcarded host added in the userlist.txt, take action.
I really don't know what to do to get this working in the script.


RE: Matching multiple parts in string - Gribouillis - Jun-07-2022

(Jun-07-2022, 04:04 PM)fozz Wrote: I still can't get this to work,
Why can't you get this to work? can you give an example of a pattern with wildcards and a user string that should match and that don't or that should not match and that do match?


RE: Matching multiple parts in string - fozz - Jun-07-2022

I wrote that in the above post.
Thank you, fozz


RE: Matching multiple parts in string - Gribouillis - Jun-07-2022

It is because you are matching the wrong patterns, for example
>>> from fnmatch import fnmatch
>>> fnmatch('[email protected]', '*!*@*.contaboserver.net')
False
>>> fnmatch('[email protected]', '**@*.contaboserver.net')
True
So what is the exclamation mark supposed to do in the pattern? You must be very precise in describing the issues. A single character in the pattern suffices to change the result.