regex searching hangs

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

    regex searching hangs

    Could someone explains why the following code hangs (Python 2.3.3)?

    --- CODE STARTS ---
    import re

    p=re.compile(r' \S+(?:\.\S+)+\. com')

    t='............ ............... ...........'
    p.search(t)
    --- CODE ENDS ---

    From the syntax and semantics of regex, the entire t should be
    consumed by the pattern '\S+(?:\.\S+)+' and the search() call should
    return None. Is search() doing a depth-first search and can't pulling
    itself out? It doesn't seem right to me, or I'm missing something?

    BTW, I don't think any regex matching should ever hang in any
    circumstances - correct me if I'm wrong: is halting problem a problem
    here?

    Thanks!
  • Jan Erik Breimo

    #2
    Re: regex searching hangs


    "Fortepianissim o" <fortepianissim o@yahoo.com.tw> wrote in message
    news:ef08387c.0 402080729.62163 2ca@posting.goo gle.com...[color=blue]
    > Could someone explains why the following code hangs (Python 2.3.3)?
    >
    > --- CODE STARTS ---
    > import re
    >
    > p=re.compile(r' \S+(?:\.\S+)+\. com')
    >
    > t='............ ............... ...........'
    > p.search(t)
    > --- CODE ENDS ---
    >[/color]

    The search doesn't hang, it just takes a long time to compute - the time it
    takes almost doubles for each dot you add to t.
    If you replace \S in the expression with [^ \t\n\r\f\v.] (ie. no whitespace
    and no dots) you'll get the computation time you expect.


    Comment

    • Rob Renaud

      #3
      Re: regex searching hangs

      fortepianissimo @yahoo.com.tw (Fortepianissim o) wrote in message news:<ef08387c. 0402080729.6216 32ca@posting.go ogle.com>...[color=blue]
      > Could someone explains why the following code hangs (Python 2.3.3)?
      >
      > --- CODE STARTS ---
      > import re
      >
      > p=re.compile(r' \S+(?:\.\S+)+\. com')
      >
      > t='............ ............... ...........'
      > p.search(t)
      > --- CODE ENDS ---
      >
      > From the syntax and semantics of regex, the entire t should be
      > consumed by the pattern '\S+(?:\.\S+)+' and the search() call should
      > return None. Is search() doing a depth-first search and can't pulling
      > itself out? It doesn't seem right to me, or I'm missing something?
      >
      > BTW, I don't think any regex matching should ever hang in any
      > circumstances - correct me if I'm wrong: is halting problem a problem
      > here?
      >
      > Thanks![/color]


      It doesn't halt, it just seems you found an exponential time search,
      searching at length i seems to take about 1.6 times the time it takes
      to search at length i-1.

      import re
      import time
      p=re.compile(r' \S+(?:\.\S+)+\. com')
      t = '.'
      for i in range(100):
      start = time.time()
      p.search(t*i)
      print i, time.time() - start

      How should you improve it? I don't know, I can't even read the regexp
      :)

      Comment

      Working...