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

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

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. THY
    Replies:
    1
    Views:
    528
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Kathy Burke
    Replies:
    3
    Views:
    582
    Kathy Burke
    Nov 24, 2003
  3. goinoutwest
    Replies:
    0
    Views:
    564
    goinoutwest
    Aug 5, 2004
  4. Christoph Krammer

    re.sub does not replace all occurences

    Christoph Krammer, Aug 7, 2007, in forum: Python
    Replies:
    3
    Views:
    689
    Christoph Krammer
    Aug 7, 2007
  5. Ben
    Replies:
    2
    Views:
    1,218
  6. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,947
    Lawrence D'Oliveiro
    May 20, 2011
  7. John Gordon
    Replies:
    13
    Views:
    714
    Ian Kelly
    Dec 20, 2011
  8. Replies:
    3
    Views:
    304
    Brian McCauley
    Sep 12, 2005
Loading...