Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick help with regular expressions
#7
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]

?
Reply


Messages In This Thread
Quick help with regular expressions - by Fiel - 2009-07-06, 01:56 PM
Quick help with regular expressions - by Dusk - 2009-07-06, 02:25 PM
Quick help with regular expressions - by Stereo - 2009-07-06, 02:34 PM
Quick help with regular expressions - by Dusk - 2009-07-06, 02:39 PM
Quick help with regular expressions - by Nikkey - 2009-07-06, 02:45 PM
Quick help with regular expressions - by Fiel - 2009-07-06, 03:12 PM
Quick help with regular expressions - by Nikkey - 2009-07-06, 03:22 PM
Quick help with regular expressions - by Fiel - 2009-07-06, 03:45 PM
Quick help with regular expressions - by Stereo - 2009-07-07, 12:30 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)