The stupid question is the question not asked | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Note that, in fact, moritz's improved version is essential—without it, your match has the potential to find your replaced text (since the s/// resets pos, so that the //g match starts all over again). For example, if you try your code (even suitably modified) on $toTranslate = 3 x 33332, then you'll get [[4]2], not [33332].
Note also that Perl goes out of its way to make sure that you can do lots of things that seem like they must be multi-step in a single statement. Thus, as moritz implicitly observed, it's probably better to write $tmpFound = length($1) (well, and use a better name) than $tmpFound = $1; $tmpFound = length($tmpFound). UPDATE: On testing, moritz's version does the same thing. I think that you want something like
UPDATE 2: eye and AnomalousMonk pointed out a better, s///g-based solution: just omit the while. In reply to Re: Regex exact pattern match problem!
by JadeNB
|
|