The other thing that complicates this a bit is I'm not showing all the original text with the words highlighted in the final output, but rather just a few words on either side of the highlighted text. Like a google search shows. Thus, I also need to be careful not to print words twice when highlighted words are close together.
That is the reason I split the source text into an array of "swish" words and non swish words -- so I could easily mark words on either side of the matched word.
@words = split /([^$wordchars])/, $source_text;<p>
I use two arrays to track this now (instead of an array of arrays) as it was faster to avoid all the dereferencing.
Here's a specific question: The "problem" with the above code is that I still need to remove the leading and trailing ignore characters. So, that's an extra pattern match for every word - one time for the split, and then another to extract out a word from its ignore chars.
I tried to find an expression to use in the above spilt that would do this in one shot, but it was looking like a complicated expression that might be slower than doing two matches. But I never found a pattern that I could test.
I also wonder if using a repeating pattern with /g might be faster than my word-by-word checking. But then I'm back to the problem of how to print the words around the match.
Thanks again for your help.
In reply to Re: Re: Optimizing regular expressions
by moseley
in thread Optimizing regular expressions
by moseley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |