Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > replace a string delimited by 2 other string, regexp problem

Reply
Thread Tools

replace a string delimited by 2 other string, regexp problem

 
 
Sébastien Maurette
Guest
Posts: n/a
 
      10-02-2006
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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      10-02-2006
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/Programming...nguage.html#UJ

 
Reply With Quote
 
 
 
 
Vincent Fourmond
Guest
Posts: n/a
 
      10-02-2006

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

 
Reply With Quote
 
David Vallner
Guest
Posts: n/a
 
      10-02-2006
--------------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(righ t)})/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--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
How to write replace string for object which will be substituted?[regexp] ryniek90 Python 1 08-06-2009 08:16 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
convert non-delimited to delimited RyanL Python 6 08-28-2007 12:06 AM
how to replace a string by other string in a text file? Umesh C Programming 17 06-21-2007 04:40 PM



Advertisments