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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
jwzumwalt
Forum Newbie
Posts: 5
Joined: Mon Dec 08, 2008 8:50 pm

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

Post 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
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

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

Post 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);
User avatar
jwzumwalt
Forum Newbie
Posts: 5
Joined: Mon Dec 08, 2008 8:50 pm

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

Post 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
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

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

Post 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";}
User avatar
jwzumwalt
Forum Newbie
Posts: 5
Joined: Mon Dec 08, 2008 8:50 pm

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

Post by jwzumwalt »

You are correct! This is is optimized. Thanks for pointing that out...

Code: Select all

(^.*\|)
Post Reply