Hostmask matching

N

Nexu

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?
 
J

John Machin

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
 
M

Marc Schoechlin

Hi !

Nexu said:
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!~.*[email protected]"

Best regards

Marc Schoechlin
 
N

Nexu

Hi !



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

A correct regular expression should look like this:
".*one!~.*[email protected]"
Thx for everyones input.

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

John Machin

Thx for everyones input.

This solved the problem:
host = '[email protected]'
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!~.*[email protected]'
|>>> host = '[email protected]'
|>>> bool(re.search(newmask, host))
True
|>>> host2 = '[email protected]'
|>>> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,283
Latest member
SherriP988

Latest Threads

Top