Python Forum
Matching multiple parts in string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching multiple parts in string
#11
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?
Reply
#12
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.
Reply
#13
(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?
Reply
#14
Have you tried using standard module fnmatch?
>>> from fnmatch import fnmatch
>>> fnmatch("123.456.987", "*.456.*")
True
DeaD_EyE likes this post
Reply
#15
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?
Reply
#16
(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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#17
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.
Reply
#18
(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?
Reply
#19
I wrote that in the above post.
Thank you, fozz
Reply
#20
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching string from a file tester_V 5 689 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  splitting file into multiple files by searching for string AlphaInc 2 1,126 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Save multiple Parts of Bytearray to File ? lastyle 1 1,050 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  matching a repeating string Skaperen 2 1,396 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,298 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Search multiple CSV files for a string or strings cubangt 7 8,563 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Matching Exact String(s) Extra 4 2,109 Jan-12-2022, 04:06 PM
Last Post: Extra
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,590 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
Question How to extract multiple text from a string? chatguy 2 2,545 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 6,096 Sep-30-2020, 02:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020