473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Hostmask matching

I'm trying to write a def to match a string that is an irc hostmask. eg:
*one!~*name@a??-??-101-101.someisp.com
But using re.search(). I get an error when the string starts with '*'.
What is the best way to solve this?
Jun 4 '06 #1
4 1888
On 4/06/2006 1:57 PM, Nexu wrote:
I'm trying to write a def
Perhaps you mean a function?
to match a string that is an irc hostmask. eg:
*one!~*name@a??-??-101-101.someisp.com
But using re.search().
If you want to find an IRC hostmask in some longer string, yes, use
re.search(). However if you want to check that a given string could be
an IRC hostmask, use re.match().
I get an error when the string starts with '*'.
Do you get an error when the searched string starts with anything else?
What is the best way to solve this?


A reasonable way would be to ask a question that includes the minimum
useful information, so that would-be helpers are not forced to guess:

1. your inputs (the re pattern, and the searched string)
2. the expected outcome (match object that spans which part of the
searched string)
3. the actual outcome (copy/paste of the traceback and error message
that you got)

Another way might be to Read The Fantastic Manual, in particular the
section on re syntax, and nut out the answer your self.

In any case, here's my guess as to what's going down:

1. You mean that the pattern starts with '*', not the searched string.
2. The error that you got was something like this:

[snip]
File "C:\Python24\lib\sre.py", line 180, in compile
return _compile(pattern, flags)
File "C:\Python24\lib\sre.py", line 227, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

3. You haven't read this part of The Fantastic Manual:

"\"
Either escapes special characters (permitting you to match characters
like "*", "?", and so forth), or ...

===

As well as TFManual, there's also a HOWTO:
http://www.amk.ca/python/howto/regex/
HTH,
John
Jun 4 '06 #2
Hi !

Nexu <ne******@gmail.com> schrieb:
I'm trying to write a def to match a string that is an irc hostmask. eg:
*one!~*name@a??-??-101-101.someisp.com
But using re.search(). I get an error when the string starts with '*'.
What is the best way to solve this?


I suppose the problem occurs because you expression is not a valid
regular expression.

A correct regular expression should look like this:
".*one!~.*name@a..-..-101-101.someisp.com"

Best regards

Marc Schoechlin

--
I prefer non-proprietary document-exchange.
http://sector7g.wurzel6.de/pdfcreator/
http://www.prooo-box.org/
Contact me via jabber: ms@256bit.org
Jun 4 '06 #3
On Sun, 2006-06-04 at 06:26 +0000, Marc Schoechlin wrote:
Hi !

Nexu <ne******@gmail.com> schrieb:
I'm trying to write a def to match a string that is an irc hostmask. eg:
*one!~*name@a??-??-101-101.someisp.com
But using re.search(). I get an error when the string starts with '*'.
What is the best way to solve this?


I suppose the problem occurs because you expression is not a valid
regular expression.

A correct regular expression should look like this:
".*one!~.*name@a..-..-101-101.someisp.com"

Thx for everyones input.

This solved the problem:
host = 's***************@a80-80-101-101.someisp.com'
mask = '*one!~*name@a??-??-101-101.someisp.com'
newmask = re.sub('\*', '.*', re.sub('\?', '.', mask))
result in that:
re.search(newmask, host) == True
Jun 4 '06 #4
On 4/06/2006 4:45 PM, Nexu wrote:
On Sun, 2006-06-04 at 06:26 +0000, Marc Schoechlin wrote:
Hi !

Nexu <ne******@gmail.com> schrieb:
I'm trying to write a def to match a string that is an irc hostmask. eg:
*one!~*name@a??-??-101-101.someisp.com
But using re.search(). I get an error when the string starts with '*'.
What is the best way to solve this?

I suppose the problem occurs because you expression is not a valid
regular expression.

A correct regular expression should look like this:
".*one!~.*name@a..-..-101-101.someisp.com"

Thx for everyones input.

This solved the problem:
host = 's***************@a80-80-101-101.someisp.com'
mask = '*one!~*name@a??-??-101-101.someisp.com'
newmask = re.sub('\*', '.*', re.sub('\?', '.', mask))
result in that:
re.search(newmask, host) == True


For a start, you must mean bool(re.search(newmask, host)) == True,
because re.search() returns a MatchObject or None; neither of those will
ever compare equal to True.

Moving right along, you have only one facet of your multi-faceted
multi-level problem fixed. Consider the following:

|>>> newmask
'.*one!~.*name@a..-..-101-101.someisp.com'
|>>> host = 's***************@a80-80-101-101.someisp.com'
|>>> bool(re.search(newmask, host))
True
|>>> host2 = 's***************@a80-80-101-101.someisp.communication.problem'
|>>> bool(re.search(newmask, host2))
True
|>>> host3 = 'someone!~thename@a80-80-101-101XsomeispYcom'
|>>> bool(re.search(newmask, host3))
True

You didn't answer either of my questions that would have told me whether
host2 is a problem; if it is, you need a '$' at the end of newmask.

To fix the host3 problem, you need '\.' instead of '.'.

There is another possible host2-like problem: if you have a hostmask
that starts with 'one' (i.e. no '*' at the front), what you are doing
now will give True for incoming starting with 'anyone!' or
'I_am_the_one!' or whatever. I don't think you want that to happen. Two
solutions: (1) Put '^' at the start of newmask (2) use re.match()
instead of re.search().

Another question: should you be doing a case-insensitive match? If so,
you need re.search/match(newmask, host, re.IGNORECASE)

You may wish to consider looking at the fnmatch module, at three levels:
(1) calling fnmatch.fnmatchcase() may be good enough for your purpose
(2) you can use the undocumented fnmatch.translate(), like this:
newmask = fnmatch.translate(mask)
and use re.match()
(3) you can find the source code in
<YOUR_PYTHON_INSTALLATION_DIRECTORY>/Lib/fnmatch.py,
copy the translate function, and rip out the lines that treat '[' as
special.

HTH,
John
Jun 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
17
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
1
by: solarin | last post by:
Hi, I've developed a program under VS 6.0. I can compile it and run it, but when I try to debbug , all my breakpoints are dissabled and I can see the following messages: Loaded...
2
by: santhoshs | last post by:
Hello I am required to parse two files that contain email addresses and figure out a way to get the matching and non-matching email addresses from both the files. I was able to get the matching...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.