Regex multiline weirdness

(Derek Wyatt) #1

This one's got me stumped.

I'm trying to rip out incomplete HTML tags from the end of a
string... here's an example:

Here is a string with <a\nhref=http://somewhere.com/and/then

Note that there is a newline in there. So i try something simple, like:

~ str.sub(/<[^>]*?$/m, "")

but it doesn't do squat. Interestingly enough, i tried the
following in perl, which i think is the direct translation:

~ $str =~ s/<[^>]*?$//s

and it works just fine.

Anyone know how to get this to work in ruby?

Thanks,
D

- --
Derek Wyatt - C++ / Ruby / Unix Programmer
http://derekwyatt.org

(David A. Black) #2

Hi --

This one's got me stumped.

I'm trying to rip out incomplete HTML tags from the end of a
string... here's an example:

Here is a string with <a\nhref=http://somewhere.com/and/then

Note that there is a newline in there. So i try something simple, like:

~ str.sub(/<[^>]*?$/m, "")

You don't need /m here. /m has the effect of adding \n to . (the
dot). Since you're specifying [^>], that already includes \n.

And as Nikolai mentioned, you can use \Z (or \z, the difference being
that \Z ignores a final \n) to reach the absolute end of string.

So... try this:

   str.sub(/<[^>]*\Z/,"")

David

···

On Mon, 22 Aug 2005, Derek Wyatt wrote:

--
David A. Black
dblack@wobblini.net

(Derek Wyatt) #3

Thanks David and Nikolai, the \Z works as advertized :slight_smile: I just have
to go through the adverts now and make sure i've got them all so i
don't have to ask stupid questions :slight_smile:

Regs,
Derek

David A. Black wrote:

Hi --

This one's got me stumped.

I'm trying to rip out incomplete HTML tags from the end of a
string... here's an example:

Here is a string with <a\nhref=http://somewhere.com/and/then

Note that there is a newline in there. So i try something

simple, like:

~ str.sub(/<[^>]*?$/m, "")

You don't need /m here. /m has the effect of adding \n to . (the
dot). Since you're specifying [^>], that already includes \n.

And as Nikolai mentioned, you can use \Z (or \z, the difference being
that \Z ignores a final \n) to reach the absolute end of string.

So... try this:

  str.sub(/<[^>]*\Z/,"")

David

- --
Derek Wyatt - C++ / Ruby / Unix Programmer
http://derekwyatt.org

···

On Mon, 22 Aug 2005, Derek Wyatt wrote: