Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: removing rest of the string after encountering a certain character

Tags:

string

regex

perl

I am doing some clean up of data in a file. So, I read each line and check certain conditions and perform the appropriate operation in the file. One of the things I need to do is check for the occurrence of the character $ in the string. If found I need to delete the rest of the line including the $. Example, if the line is

abc$hello-goodbye

I need to get

abc

How do I do this in Perl with minimal code? Use regexp of some sort?

like image 901
sfactor Avatar asked Dec 13 '22 14:12

sfactor


1 Answers

Quickest is by an easy regular expression:

$string =~ s/\$.*//;

where

$string = 'abc$hello-goodbye';
like image 61
darklion Avatar answered Jan 05 '23 01:01

darklion