[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex



On Fri, Jan 14, 2000 at 10:56:42AM -0500, Trevor R.H. Clarke wrote:
> This is not an openbsd problem but maybe someone can help anyway...I've got a
> string in the form [NSEW]+ It is a series of directions that are repeatedly
> followed until a goal is reached thus EEESEEES is essentially the same as EEES
> and EEE is essentially the same as E. I'm writeing a function (in python) to
> reduce these strings to simplest form as above. I'm trying to use python's
> regex matching functions(the same as perl regex's)to do this.
> My first thought was to do the following where s is the string, sl is the len
> of s, and x is the set of divisors of sl.
> 
> y = sl / x
> m = re.match("([NSEW]{%i}){%i}" % (x,y),s)
> 
> the problem is, the regex(using x is 2 and y is 4 for example) ([NSEW]{2}){4}
> evaluates the same as [NSEW]{8} which will match any a character string and
> this is obviously not the desired result. Is there a way to pre-evaluate a
> regex group? Any other suggestions on how to do this or am I gonna have to do
> it the long way?

It's an interesting brain teaser, though I think the "long way" is 
probably more efficient.

If python supports back-references in regex, like perl, this should
match it:
  ([NSEW]{x})(\1{y-1})
or in your example:
  m = re.match("([NSEW]{%i})\1{%i}" % (x,y-1),s)

  -- Clifton

-- 
 Clifton Royston  --  LavaNet Systems Architect --  cliftonr@lava.net
        "An absolute monarch would be absolutely wise and good.  
           But no man is strong enough to have no interest.  
             Therefore the best king would be Pure Chance.  
              It is Pure Chance that rules the Universe; 
          therefore, and only therefore, life is good." - AC