in reply to Re: Regex infinite loop?
in thread Regex infinite loop?

I think I need to give everyone more to go on, so here it is. My matching code looks like the following.

while (content =~ m%One(.*?)three(.?)five\s+six(.*?)this%gs) { $var1 = $1 ; $var2 = $2 ; $var3 = $3 ; print "$var1\t$var2\t$var3\n" ; }

It gives me output that looks like you would expect, say, for example:

two   four   seven

When the code hangs, it prints out the same thing, but then nothing more.

Now, I've made the following change in keeping with your suggestion.

while (content =~ m%One(.*?)three(.?)five\s+six(.*?)this%gs) { print(STDERR "[", pos($content), "]") ; $var1 = $1 ; $var2 = $2 ; $var3 = $3 ; print "$var1\t$var2\t$var3\n" ; }

I'm relatively new to PERL and not a professional coder, so I'm not sure what this is supposed to produce, but here's an example of what I am getting.

[ two four seven 48430]

On another match, however, it gives me:

[48226] three more numbers

When it hangs, it gives me the following.

[50757] first three numbers second three numbers third three numbers [51826][52896]

Does this help with understanding the problem?

Replies are listed 'Best First'.
Re^3: Regex infinite loop?
by moritz (Cardinal) on Oct 16, 2008 at 19:30 UTC
    Why are you so reluctant to give us the regex (and not a similar regex), the data on which it hangs, and the rest of the program that might hang?

    That leaves us pointlessly fishing in the fog.

    "Sir, can you help me? my car is too slow" - "So, what kind of car is it?" - "a black one"

      Sorry about that -- I am not a professional, so I guess I would get a bunch of grief for lack of elegance. Anyway, I would rather be embarrassed and have working code than not be embarrassed and have code that doesn't work. I included the code in another reply on this thread. Thanks.

Re^3: Regex infinite loop?
by ikegami (Patriarch) on Oct 16, 2008 at 20:45 UTC

    Now, I've made the following change in keeping with your suggestion.

    No you didn't. You moved the print statement.

    Also, is that really the code you tested with?

    while (content =~ m%One(.*?)three(.?)five\s+six(.*?)this%gs) { ^^^^^^^

    A function that's called over and over and over? I doubt you'd get the output you got if that was the case. Since you're showing us code that has no relevance to yours, it's hard to help.

    If you try again, I'd switch to using the following:

    $|=1; while ( print("[", pos($content), "]"), $content =~ m%One(.*?)three(.?)five\s+six(.*?)this%gs ) { $var1 = $1 ; $var2 = $2 ; $var3 = $3 ; print "$var1\t$var2\t$var3\n" ; }

    But an educated guess on what I've seen leads me to think it's not a problem with your loop.