Hostmask matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nexu

    #1

    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?


  • John Machin

    #2
    Re: Hostmask matching

    On 4/06/2006 1:57 PM, Nexu wrote:[color=blue]
    > I'm trying to write a def[/color]

    Perhaps you mean a function?
    [color=blue]
    > to match a string that is an irc hostmask. eg:
    > *one!~*name@a??-??-101-101.someisp.com
    > But using re.search().[/color]

    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().
    [color=blue]
    > I get an error when the string starts with '*'.[/color]

    Do you get an error when the searched string starts with anything else?
    [color=blue]
    > What is the best way to solve this?
    >[/color]

    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\li b\sre.py", line 180, in compile
    return _compile(patter n, flags)
    File "C:\Python24\li b\sre.py", line 227, in _compile
    raise error, v # invalid expression
    sre_constants.e rror: 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:



    HTH,
    John

    Comment

    • Marc Schoechlin

      #3
      Re: Hostmask matching

      Hi !

      Nexu <nexu.jin@gmail .com> schrieb:[color=blue]
      > 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?[/color]

      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.

      OpenOffice.org, Open Office, OpenOffice, openoffice, Apache Openoffice, Apache OpenOffice, ApacheOpenOffice, apacheopenoffice

      Contact me via jabber: ms@256bit.org

      Comment

      • Nexu

        #4
        Re: Hostmask matching

        On Sun, 2006-06-04 at 06:26 +0000, Marc Schoechlin wrote:[color=blue]
        > Hi !
        >
        > Nexu <nexu.jin@gmail .com> schrieb:[color=green]
        > > 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?[/color]
        >
        > 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 "[/color]
        Thx for everyones input.

        This solved the problem:
        host = 'someone!~thena me@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(newma sk, host) == True


        Comment

        • John Machin

          #5
          Re: Hostmask matching

          On 4/06/2006 4:45 PM, Nexu wrote:[color=blue]
          > On Sun, 2006-06-04 at 06:26 +0000, Marc Schoechlin wrote:[color=green]
          >> Hi !
          >>
          >> Nexu <nexu.jin@gmail .com> schrieb:[color=darkred]
          >>> 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?[/color]
          >> 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 "[/color]
          > Thx for everyones input.
          >
          > This solved the problem:
          > host = 'someone!~thena me@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(newma sk, host) == True[/color]

          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 = 'someone!~thena me@a80-80-101-101.someisp.com '
          |>>> bool(re.search( newmask, host))
          True
          |>>> host2 = 'someone!~thena me@a80-80-101-101.someisp.com munication.prob lem'
          |>>> bool(re.search( newmask, host2))
          True
          |>>> host3 = 'someone!~thena me@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.fnmatch case() may be good enough for your purpose
          (2) you can use the undocumented fnmatch.transla te(), like this:
          newmask = fnmatch.transla te(mask)
          and use re.match()
          (3) you can find the source code in
          <YOUR_PYTHON_IN STALLATION_DIRE CTORY>/Lib/fnmatch.py,
          copy the translate function, and rip out the lines that treat '[' as
          special.

          HTH,
          John

          Comment

          Working...