--- In [email protected], "Owen Watson" <[EMAIL PROTECTED]> wrote: > > Sheri, I'm having the same performance problems with this as I was > with Bruce's original regex implementation. The main culprit is this: > > textIn = regex.pcreReplace(?".+</head>",textIn,?"") > > ie I'm deleting the header of a html file. > > Is it the syntax or just to be expected? > > The amount a data in textIn isn't great, only about 40K. >
Hi Owen, The problem is the way you've expressed your pattern. I assume textIn must have no newlines in it, so the .+ matches the entire string. Then PCRE it has to to backtrack one character at a time to try to match "</head>" Since </head> is not near the end of the string, it will take a long time. Also, the way you have it, it is not just deleting the header of the file. It is deleting everything from the beginning thru </head>. Again, this is assuming your textin has no newlines in it. If it does, the newlines don't match dot without a special option, so it would only match from the beginning of the line containing </head> thru the last </head> found on that line. It would still be slow, because it has to test from every dot position on every line to see if .+ has a </head> after it. I would do it this way: regex.pcreReplace(?"<head>(.+?)</head>", textIn, "", "dotall target:1") That will look first for <head>. If it finds <head> it will look for the minimum number of characters to match </head> So there should be no backtracking. Once it finds </head>, its done. Now the replace, the way I did it above, thanks to target:1 it is only going to replace the part between the tags with an empty string. It wouldn't matter if it was spread out over multiple lines because of the dotall option. Dotall can also be specified as part of the pattern, using (?s). If you want to remove the whole match including the tags, you could do it this way: regex.pcreReplace(?"(?s)<head>.+?</head>", textIn, "") Altho the above won't be slow, you might further improve performance by specifying that only a maximum of 1 match is to be processed: regex.pcreReplace(?"<head>.+?</head>", textIn, "", "dotall", 1) You can find out how many replacements were processed after a replacement by inspecting regex_count. Hope it helps, Sheri
