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
search from end of line? or (remove after last delimiter)
Moderator: General Moderators
- 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)
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)
Thank you for your quick solution. I also came up with the solution that follows...
Thanks for the help! Jan Zumwalt
Code: Select all
if ($line_in =~ m/(^*.*\|)/) {print "$1\n";}- 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)
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)
You are correct! This is is optimized. Thanks for pointing that out...
Code: Select all
(^.*\|)