re.sub do not replace portion of match

Discussion in 'Python' started by J Wolfe, Oct 2, 2009.

  1. J Wolfe

    J Wolfe Guest

    Hi,

    Is there a way to flag re.sub not to replace a portion of the string?

    I have a very long string that I want to add two new line's to rather
    than one, but keep the value X:

    string = "test........X.\n.today" <-- note X is a value
    string = re.sub("test........X.\n.","test........X.\n\n.", string)

    This just replaces X with the replacement string.

    Thanks,
    Jonathan
     
    J Wolfe, Oct 2, 2009
    #1
    1. Advertising

  2. J Wolfe

    J Wolfe Guest

    Thanks Duncan,

    I did look at that, but it was kinda greek to me. Thanks for pulling
    out the part I was looking for that should do the trick.

    Jonathan

    > http://www.python.org/doc/current/library/re.html#re.sub
    >
    > > Backreferences, such as \6, are replaced with the substring matched by
    > > group 6 in the pattern.
     
    J Wolfe, Oct 2, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. goinoutwest
    Replies:
    0
    Views:
    490
    goinoutwest
    Aug 5, 2004
  2. Ben
    Replies:
    2
    Views:
    892
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,036
    Lawrence D'Oliveiro
    May 20, 2011
  4. John Gordon
    Replies:
    13
    Views:
    489
    Ian Kelly
    Dec 20, 2011
  5. Replies:
    3
    Views:
    174
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page