Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick help with regular expressions
#1
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?
Reply
#2
replace [noparse]/#b([\w ])#k/ with /\Q\E\1\Q/.[/noparse]

Does Python have a substitution function?

sed/anything similar = [noparse]s/#b([\w ])#k/\Q\E\1\Q/g;[/noparse]
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.
Reply
#3
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".
Reply
#4
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.
Reply
#5
[noparse]re.sub("#b(.*?)#k", "\\1", x)[/noparse]

brb dinner.
Reply
#6
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.
Reply
#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
#8
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]
Reply
#9
Isn't #b for blue text anyway? I think bold is #e.
Reply
#10
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 =)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)