![]() |
|
Quick help with regular expressions - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Quick help with regular expressions (/showthread.php?tid=13206) |
Quick help with regular expressions - Fiel - 2009-07-06 Okay, here is my string I'm working with. "Todd taught me to move towards upper right-hand side to attack and kill #bJr. Sentinel#k" I want it to turn into this: "Todd taught me to move towards upper right-hand side to attack and kill [noparse]Jr. Sentinel[/noparse]" Here's how I'm currently doing it: Regular expression --> #b[\w ]*#k Then snip off the ends (#b and #k) Then add my tags ([noparse] and [/noparse]) Then do a string replace("[noparse]Jr. Sentinel[/noparse]" for "#bJr. Sentinel#k") There's got to be a faster, more efficient way of doing this in Python. Any ideas? Quick help with regular expressions - Dusk - 2009-07-06 replace [noparse]/#b([\w ])#k/ with /\Q\E\1\Q/.[/noparse] Does Python have a substitution function? sed/anything similar = Actually I guess that should be [noparse]s:#b(.*?)#k:\1:g[/noparse] which looks much nicer. Explanation: \Q starts a literal string, \E ends it. When you place a segment of a regex in parentheses it gets stored in the buffers \1..\9. Quick help with regular expressions - Stereo - 2009-07-06 import re re.sub(#b([\w ]+)#k,$1,string) etc. I'm not sure if you need to escape the [] in the replacement string cause I tested it using some online thingy. And I don't know if \w will match a "." which is in Jr. Sentinel. Might just want [.] Brackets() let you refer to each match by $1, consecutive brackets are $2 etc. like if you look for (#[br])([\w ]*)#[kb] then $1 would be a b, $2 the "Jr. Sentinel". Quick help with regular expressions - Dusk - 2009-07-06 Stereo Wrote:Might just want [.] Oh, it doesn't. Shouldn't that be [\.\w ] though? It'd mess up if there's more than one tag on a line, i.e. #bTodd#k taught me to move towards upper right-hand side to attack and kill #bJr. Sentinel#k edit: k that does it below lol. Do global find/replace though. Quick help with regular expressions - Nikkey - 2009-07-06 [noparse]re.sub("#b(.*?)#k", "\\1", x)[/noparse] brb dinner. Quick help with regular expressions - Fiel - 2009-07-06 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. Quick help with regular expressions - Nikkey - 2009-07-06 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. (.*?) 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] ? Quick help with regular expressions - Fiel - 2009-07-06 Devil's Sunrise Wrote:[noparse]x.replaceAll("#b", "").replaceAll("#k", "")[/noparse] Because that would break other expressions. "#rTodd#k taught me to move towards upper right-hand side to attack and kill #bJr. Sentinel#k" where #r#k = [noparse][highlight][/highlight][/noparse] Quick help with regular expressions - Stereo - 2009-07-07 Isn't #b for blue text anyway? I think bold is #e. Quick help with regular expressions - KajitiSouls - 2009-07-07 Stereo Wrote:Isn't #b for blue text anyway? I think bold is #e. I'm with Stereo, because my friend sees #e all over the place in the ToS stuff, which has no blue text in sight, but bolds in various places. I should add that the Guest ToS (if there are other ToS versions, I dunno) is easily available since all you have to do to see it is to click the Guest Login at the MS Login screen =) |