replace a string delimited by 2 other string, regexp problem

Discussion in 'Ruby' started by Sébastien Maurette, Oct 2, 2006.

  1. Hello,

    I want to replace a string in a file who is delimited by 2 other string.
    My problem is to find the start and the end string delimiter and replace
    the content between both.

    Ex:
    str = " /* startdel1 */ text1 text1 text1 /*enddel1*/ /*startdel2*/
    text2 text2 text2 /*enddel2*/ /*startdel3*/ text3 text3 text3
    /*enddel3*/ "

    In this example i'want to replace the string /*startdel2*/ text2 text2
    text2 /*enddel2*/
    by

    /*startdel2*/ hello /*enddel2*/

    i'm newbie and it's a real problem for me to do that.
    help me
    thx





    ___________________________________________________________________________
    Yahoo! Mail réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son interface révolutionnaire.
    http://fr.mail.yahoo.com
     
    Sébastien Maurette, Oct 2, 2006
    #1
    1. Advertising

  2. On Tue, Oct 03, 2006 at 04:55:44AM +0900, S?bastien Maurette wrote:
    > Hello,
    >
    > I want to replace a string in a file who is delimited by 2 other string.
    > My problem is to find the start and the end string delimiter and replace
    > the content between both.
    >

    Learn about regular expressions, and then look into the gsub method of
    String. You can get a very brief intro to regular expressions here:
    http://ruby-doc.org/docs/ProgrammingRuby/html/language.html#UJ
     
    Logan Capaldo, Oct 2, 2006
    #2
    1. Advertising

  3. Hello !

    > Ex:
    > str = " /* startdel1 */ text1 text1 text1 /*enddel1*/ /*startdel2*/
    > text2 text2 text2 /*enddel2*/ /*startdel3*/ text3 text3 text3
    > /*enddel3*/ "
    >
    > In this example i'want to replace the string /*startdel2*/ text2 text2
    > text2 /*enddel2*/
    > by
    >
    > /*startdel2*/ hello /*enddel2*/


    left = "/*startdel2*/"
    right = "/*enddel2*/"
    p str.gsub(/#{Regexp.quote(left)}(.*?)#{Regexp.quote(right)}/m,
    "#{left}hello#{right}")

    You need the /m as there might be newlines in the match, and you need
    the Regexp.quote as your strings contain characters which lose their
    usual meaning in a regexp (*).

    Cheers !

    Vince
     
    Vincent Fourmond, Oct 2, 2006
    #3
  4. --------------enig817FD8C1571506A4A77A1FC8
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable

    Vincent Fourmond wrote:
    > left =3D "/*startdel2*/"
    > right =3D "/*enddel2*/"
    > p str.gsub(/#{Regexp.quote(left)}(.*?)#{Regexp.quote(right)}/m,
    > "#{left}hello#{right}")
    >=20


    The "right" could be in a positive lookahead making things probably a
    likkle bit faster. No lookbehinds until Ruby 1.9 / 2.0 though.

    p str.gsub(/#{Regexp.quote(left)}(.*?)(?=3D#{Regexp.quote(right)})/m,
    "#{left}hello")

    David Vallner


    --------------enig817FD8C1571506A4A77A1FC8
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: OpenPGP digital signature
    Content-Disposition: attachment; filename="signature.asc"

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (MingW32)

    iD8DBQFFIYdFy6MhrS8astoRAvxwAJsFcYS3yog/SDsLRa7p3UfnKf1HNwCfZ5Jm
    l3f8M6Ou5SQFqXFgIe1WrZY=
    =feCN
    -----END PGP SIGNATURE-----

    --------------enig817FD8C1571506A4A77A1FC8--
     
    David Vallner, Oct 2, 2006
    #4
    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. Umesh
    Replies:
    17
    Views:
    878
  2. RyanL
    Replies:
    6
    Views:
    690
    Paul McGuire
    Aug 28, 2007
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    491
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    365
    7stud --
    Aug 21, 2009
  5. Scott Bass
    Replies:
    4
    Views:
    118
    Tad McClellan
    May 12, 2005
Loading...

Share This Page