2009-07-06, 03:22 PM
Fiel Wrote:Ah, didn't know about the buffers in the re.sub() function. No wonder why I couldn't make heads from tails on it. Devil's sub() function worked perfectly.
One question:
"(.*?)" <--- Isn't the *? redundant? Couldn't it just as well be "(.*)"? What am I missing here?
Also, Python's regex parser leans heavily on Perl. I wouldn't be surprised if Dusk's expression worked too.
(.*?) is lazy matching. Trying to find a match through (.*) would yield a greedy match (as big match as possible). Greedy matches would work okay in sentences where there are only one match, e.g.
"Todd taught me to move towards upper right-hand side to attack and kill #bJr. Sentinel#k"
However, whenever there are several matches, greedy matches would fail:
"Todd taught me to move towards upper right-hand side to attack and kill #bJr. Sentinel#k and #bDrum Bunnies#k"
Would be transformed into
[noparse]"Todd taught me to move towards upper right-hand side to attack and kill Jr. Sentinel#k and #bDrum Bunnies"[/noparse]
With the "?", you add in lazy matching, the match would translate into
[noparse]"Todd taught me to move towards upper right-hand side to attack and kill Jr. Sentinel and Drum Bunnies"[/noparse]
And as stated earlier, () are brackets, and may be referred to through litteral "\1". (Litteral --> backslashes equal backslashes. Thus, to refer to a backslash, you have to double-backslash. And to actually refer to a "\"-replacement, you'd have to write four backslashes.). \\1 thus refers to the match.
Though, why not actually just
[noparse]x.replaceAll("#b", "").replaceAll("#k", "")[/noparse]
?

