Results 1 to 10 of 10
  1. Default Quick help with regular expressions


    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 [b]Jr. Sentinel[/b]"

    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 ([b] and [/b])
    Then do a string replace("[b]Jr. Sentinel[/b]" for "#bJr. Sentinel#k")

    There's got to be a faster, more efficient way of doing this in Python. Any ideas?

  2. Default


    replace /#b([\w ])#k/ with /\Q[b]\E\1\Q[/b]/.

    Does Python have a substitution function?

    sed/anything similar = s/#b([\w ])#k/\Q[b]\E\1\Q[/b]/g;
    Actually I guess that should be s:#b(.*?)#k:[b]\1[/b]:g 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.
    Last edited by Dusk; 2009-07-06 at 02:28 PM.

  3. Default


    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".

  4. Default


    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.
    Last edited by Dusk; 2009-07-06 at 01:54 PM.

  5. DUCKS
    IGN: Mondays
    Server: Bellocan
    Level: 170
    Job: White Knight
    Guild: Affinity
    Alliance: Honour
    norway

    Default


    re.sub("#b(.*?)#k", "[b]\\1[/b]", x)

    brb dinner.

  6. Default


    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.

  7. DUCKS
    IGN: Mondays
    Server: Bellocan
    Level: 170
    Job: White Knight
    Guild: Affinity
    Alliance: Honour
    norway

    Default


    (.*?) 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
    "Todd taught me to move towards upper right-hand side to attack and kill [b]Jr. Sentinel#k and #bDrum Bunnies[/b]"

    With the "?", you add in lazy matching, the match would translate into
    "Todd taught me to move towards upper right-hand side to attack and kill [b]Jr. Sentinel[/b] and [b]Drum Bunnies[/b]"

    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

    x.replaceAll("#b", "[b]").replaceAll("#k", "[/b]")

    ?

  8. Default


    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 = [highlight][/highlight]

  9. Default


    Isn't #b for blue text anyway? I think bold is #e.

  10. Default


    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 =)

  11.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •