re quiz

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David C. Ullrich

    re quiz

    True or False? (no fair looking it up)

    (*) If repl is a string then re.sub(pattern, repl, s)
    returns s with non-overlapping occurences of pattern
    replaced by repl.

    I assumed it was true - spent a few hours trying to
    figure out what was going on with a certain re.sub,
    then noticed that (*) is false:

    (**) "If repl is a string, any backslash escapes in it are
    processed. That is, "\n" is converted to a single newline
    character, "\r" is converted to a linefeed, and so forth."

    So I changed my r"\remark{Hint} " to r"\\remark{Hint }"
    and things were fine.

    A pointless question and then a practical one.

    Pointless question: There must be a good reason for (**).
    What would it be? Seems needlessly confusing to me (of
    course a lot of things seem confusing to me...)

    Maybe it's going to be confusing no matter what they do.
    But "\\n" looks like it doesn't contain a newline, but it
    gets converted to something that does.

    (Another fascinating question is how they could phrase
    the docs here so as to confuse nobody. Because "\n"
    _is_ a newline, or so it's going to look to many people;
    I'd spell it out:: "a string containing '\' followed by 'n' ".)

    Practical question: What's a _complete_ list of the
    escapes included in the "and so forth" in (**)?

    (Or is there a function somewhere that will convert
    r"\remark{Hint} " to r"\\remark{Hint }" for me, and
    do the same for precisely the escpapes referred to
    in the "and so forth"?)


    David C. Ullrich
  • John Machin

    #2
    Re: re quiz

    On Jun 12, 8:57 pm, David C. Ullrich <dullr...@spryn et.comwrote:
    True or False? (no fair looking it up)
    >
    (*) If repl is a string then re.sub(pattern, repl, s)
    returns s with non-overlapping occurences of pattern
    replaced by repl.
    >
    I assumed it was true - spent a few hours trying to
    figure out what was going on with a certain re.sub,
    then noticed that (*) is false:
    >
    Well, the docs do say "Return the string obtained by replacing the
    leftmost non-overlapping occurrences of pattern in string by the
    replacement repl." -- care to tell us what "a certain re.sub" is, and
    false in what way?

    Comment

    • Peter Otten

      #3
      Re: re quiz

      David C. Ullrich wrote:
      (Or is there a function somewhere that will convert
      r"\remark{Hint} " to r"\\remark{Hint }" for me, and
      do the same for precisely the escpapes referred to
      in the "and so forth"?)
      I think you just have to escape the backslash:

      re.sub(pattern, replacement_str ing.replace("\\ ", "\\\\"), s)

      Anyway here's the list of troublemakers:
      >>def means_trouble(c ):
      .... try:
      .... return re.sub("x", "\\" + c, "x") != "\\" + c
      .... except:
      .... return True
      ....
      >>[c for c in map(chr, range(256)) if means_trouble(c )]
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']


      Peter

      Comment

      • Duncan Booth

        #4
        Re: re quiz

        David C. Ullrich <dullrich@spryn et.comwrote:
        Practical question: What's a _complete_ list of the
        escapes included in the "and so forth" in (**)?
        >
        (Or is there a function somewhere that will convert
        r"\remark{Hint} " to r"\\remark{Hint }" for me, and
        do the same for precisely the escpapes referred to
        in the "and so forth"?)
        >
        As the documentation says:

        Character escapes
        Numbered Groups: \0 \1 \2 \3 ...
        Named groups: \g<name>
        Numbered groups with explicit termination of the number: \g<0\g<1...

        But it doesn't matter what the complete list is. All of the escapes start
        with \ so doubling all the \\ will prevent any of them being interpreted as
        special so if you aren't wanting to substitute any groups into the string
        just try repl.replace('\ \', r'\\')


        --
        Duncan Booth http://kupuguy.blogspot.com

        Comment

        • David C. Ullrich

          #5
          Re: re quiz

          On Thu, 12 Jun 2008 05:12:55 -0700 (PDT), John Machin
          <sjmachin@lexic on.netwrote:
          >On Jun 12, 8:57 pm, David C. Ullrich <dullr...@spryn et.comwrote:
          >True or False? (no fair looking it up)
          >>
          >(*) If repl is a string then re.sub(pattern, repl, s)
          >returns s with non-overlapping occurences of pattern
          >replaced by repl.
          >>
          >I assumed it was true - spent a few hours trying to
          >figure out what was going on with a certain re.sub,
          >then noticed that (*) is false:
          >>
          >
          >Well, the docs do say "Return the string obtained by replacing the
          >leftmost non-overlapping occurrences of pattern in string by the
          >replacement repl."
          That's the _first sentence_, yes. I _quoted_ another sentence
          (from an old version, istr it phrased slightly differently in
          recent versions) in my post.
          -- care to tell us what "a certain re.sub" is, and
          >false in what way?
          Read the OP.
          David C. Ullrich

          Comment

          • David C. Ullrich

            #6
            Re: re quiz

            On Thu, 12 Jun 2008 14:12:31 +0200, Peter Otten <__peter__@web. de>
            wrote:
            >David C. Ullrich wrote:
            >
            >(Or is there a function somewhere that will convert
            >r"\remark{Hint }" to r"\\remark{Hint }" for me, and
            >do the same for precisely the escpapes referred to
            >in the "and so forth"?)
            >
            >I think you just have to escape the backslash:
            >
            >re.sub(pattern , replacement_str ing.replace("\\ ", "\\\\"), s)
            >
            >Anyway here's the list of troublemakers:
            Heh - I shoulda thought of that, determining which
            ones cause trouble by checking to see which ones
            cause trouble. Thanks.
            >>>def means_trouble(c ):
            >... try:
            >... return re.sub("x", "\\" + c, "x") != "\\" + c
            >... except:
            >... return True
            >...
            >>>[c for c in map(chr, range(256)) if means_trouble(c )]
            >['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']
            >
            >
            >Peter
            David C. Ullrich

            Comment

            • David C. Ullrich

              #7
              Re: re quiz

              On 12 Jun 2008 12:32:13 GMT, Duncan Booth
              <duncan.booth@i nvalid.invalidw rote:
              >David C. Ullrich <dullrich@spryn et.comwrote:
              >
              >Practical question: What's a _complete_ list of the
              >escapes included in the "and so forth" in (**)?
              >>
              >(Or is there a function somewhere that will convert
              >r"\remark{Hint }" to r"\\remark{Hint }" for me, and
              >do the same for precisely the escpapes referred to
              >in the "and so forth"?)
              >>
              >
              >As the documentation says:
              >
              >Character escapes
              >Numbered Groups: \0 \1 \2 \3 ...
              >Named groups: \g<name>
              >Numbered groups with explicit termination of the number: \g<0\g<1...
              Right - I was wondering about a complete list of character
              escapes.
              >But it doesn't matter what the complete list is. All of the escapes start
              >with \ so doubling all the \\ will prevent any of them being interpreted as
              >special so if you aren't wanting to substitute any groups into the string
              >just try repl.replace('\ \', r'\\')
              Good point.

              David C. Ullrich

              Comment

              • Johannes Bauer

                #8
                Re: re quiz

                David C. Ullrich schrieb:
                >-- care to tell us what "a certain re.sub" is, and
                >false in what way?
                >
                Read the OP.
                Well, aren't you funny. Maybe you should have referenced the other
                thread so one can find the OP?

                Regards,
                Johannes

                --
                "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
                reicht zu wissen, daß andere es besser können und andere es auch
                besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
                in de.sci.electron ics <47fa8447$0$115 45$9b622d9e@new s.freenet.de>

                Comment

                • David C. Ullrich

                  #9
                  Re: re quiz

                  In article <qvq7i5x726.ln2 @joeserver.home lan.net>,
                  Johannes Bauer <dfnsonfsduifb@ gmx.dewrote:
                  David C. Ullrich schrieb:
                  >
                  -- care to tell us what "a certain re.sub" is, and
                  false in what way?
                  Read the OP.
                  >
                  Well, aren't you funny. Maybe you should have referenced the other
                  thread so one can find the OP?
                  What other thread? OP is sometimes Original Poster and
                  sometimes Original Post. In the original post in this
                  very thread I gave an quote from the docs and an example
                  illustrating the answer to the question I was asked.

                  Ok, I guess it's hard to find the top of the thread.
                  I wanted to replace a certain pattern with r"\remark{Hint} ".
                  I didn't understand why that didn't work until I read
                  the bit of the docs that I quoted in my original post:

                  (**) "If repl is a string, any backslash escapes in it are
                  processed. That is, "\n" is converted to a single newline
                  character, "\r" is converted to a linefeed, and so forth."
                  Regards,
                  Johannes
                  --
                  David C. Ullrich

                  Comment

                  Working...