Click here to get back home

Regexp to search over several lines in one string

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Regexp to search over several lines in one string d99alu 01-27-2008
Get Chitika Premium
Posted by d99alu on January 27, 2008, 7:18 am
Please log in for more thread options
Hi!

I have a string, and I want to remove everything behind the ">"
character. The string contains new line characters that I don't want
to remove.

my $string = "line1
line2>
line3";

Why don't I get a match and replacement with this?

$string =~ s/^([^>]*>)/$1/;

I would expect the string to contain:

"line1
line2>"

But it still contains "line3"!!!

Why is this?
Any suggestions for how to do this in an other 8working) manner?

Best Regards,
Andreas - Sweden

Posted by Dr.Ruud on January 27, 2008, 7:39 am
Please log in for more thread options
d99alu@efd.lth.se schreef:

> I have a string, and I want to remove everything behind the ">"
> character. The string contains new line characters that I don't want
> to remove.

s/(?:<=>).*//s;

See perldoc perlre, search for "look-behind".

--
Affijn, Ruud

"Gewoon is een tijger."

Posted by John W. Krahn on January 27, 2008, 10:06 am
Please log in for more thread options
Dr.Ruud wrote:
> d99alu@efd.lth.se schreef:
>
>> I have a string, and I want to remove everything behind the ">"
>> character. The string contains new line characters that I don't want
>> to remove.
>
> s/(?:<=>).*//s;

ITYM: s/(?<=>).*//s;


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Dr.Ruud on January 27, 2008, 10:59 am
Please log in for more thread options
John W. Krahn schreef:
> Dr.Ruud:
>> d99alu:

>>> I have a string, and I want to remove everything behind the ">"
>>> character. The string contains new line characters that I don't want
>>> to remove.
>>
>> s/(?:<=>).*//s;
>
> ITYM: s/(?<=>).*//s;

Yes. (aaargh, oops again)

--
Affijn, Ruud

"Gewoon is een tijger."


Posted by Dr.Ruud on January 27, 2008, 11:11 am
Please log in for more thread options
Dr.Ruud schreef:
> d99alu:

>> I have a string, and I want to remove everything behind the ">"
>> character. The string contains new line characters that I don't want
>> to remove.
>
> s/(?:<=>).*//s;
>
> See perldoc perlre, search for "look-behind".

I also forgot the newline. Maybe this does what you need:

s/(?<=>).*/\n/s;

(doesn't keep any of the original newlines; even adds one when none was
there)

--
Affijn, Ruud

"Gewoon is een tijger."


Similar ThreadsPosted
loop over a string to do search/replacement using regexp? October 30, 2006, 5:36 pm
Search a Text File for a String, Return String to Function July 29, 2006, 12:19 pm
search/replace for consecutive blank lines March 27, 2005, 3:01 pm
unicode (hebrew) regexp search for new line headaches December 4, 2005, 10:46 pm
How can I perform search & proximity-replace over multiple lines in C source July 23, 2004, 11:33 am
How to search a string in a given line February 7, 2006, 4:12 am
Perl String Search April 19, 2006, 11:19 am
Simple string search November 1, 2007, 10:57 am
How can I strip keywords out of a search string December 11, 2004, 8:15 am
Search for string and return file name November 17, 2004, 1:13 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap