Page 1 of 1

search from end of line? or (remove after last delimiter)

Posted: Mon Nov 02, 2009 5:29 pm
by jwzumwalt
I would like to remove all (if any) characters after a delimiter at the end of the line.
The file structure is like this...
data1|data2|data...| comment

So, I need to remove all characters after the last "|"

There are a couple strategies that I thought about. I don't know if there is a way to find the first occurrence of a character FROM the end of the line, this would be the preferred solution. Another approach would be to search for the number of occurrences of "|" and cut the valid data up to it, but I thought probably Regex would have a simple one line solution.

Thanks for the help, Jan Zumwalt

Re: search from end of line? or (remove after last delimiter)

Posted: Mon Nov 02, 2009 11:16 pm
by ridgerunner
Funny you should ask. A question very similar to this one was just answered two threads ago (start search at end, use commas). Bottom line: You can use the PHP strrpos() function to search a string in the reverse direction starting from the end. Or if optimized efficiency is not an issue, you could use a regular expression such as this one for your one line solution:

Code: Select all

$text = preg_replace('/(?<=\|)[^|\r\n]+$/', '', $text);

Re: search from end of line? or (remove after last delimiter)

Posted: Tue Nov 03, 2009 12:06 am
by jwzumwalt
Thank you for your quick solution. I also came up with the solution that follows...

Code: Select all

if ($line_in =~ m/(^*.*\|)/) {print "$1\n";}
Thanks for the help! Jan Zumwalt

Re: search from end of line? or (remove after last delimiter)

Posted: Tue Nov 03, 2009 12:38 pm
by ridgerunner
Perl script? Just curious... why the * quantifier applied to the ^ beginning of string? Shouldn't it be:

Code: Select all

if ($line_in =~ m/(^.*\|)/) {print "$1\n";}

Re: search from end of line? or (remove after last delimiter)

Posted: Tue Nov 03, 2009 1:34 pm
by jwzumwalt
You are correct! This is is optimized. Thanks for pointing that out...

Code: Select all

(^.*\|)