Perl regex - can I say 'if character/string matches, delete it and all to right of it'?

14,342

Solution 1

You can delete anything after a hyphen - with this substitution:

s/-.*$//s

However, you will want to remove the whitespace prior to the hyphen and thus do

s/\s* - .* $//xs

The $ anchores the regex at the end of the string and the /s flag allows the dot to match newlines as well. While the $ is superfluous, it might add clarity.

Your substitution would just have removed the first -, and your transliteration would have removed all hyphens from the string.

Solution 2

Your regular expressions are just searching for the dash, so that's all they replace. You want to search for the dash, and anything after it.

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

. represents any character, * means search for that character 0 or more times, and match as many as possible (i.e. to the end of the string if possible)

You can also search for an optional space before it.

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

(\s is a clearer way to specify a space character)

Solution 3

Using plain substr() and index() is possible as well.

my @strings = ("we are - so cool",
               "lonely",
               "friend-Manchester",
               "home - london",
               "home-new york",
               "home with-childeren-first episode");
local $/ = " ";
foreach (@strings) {
  $_ = substr($_,0,index($_,'-')) if (index($_,'-') != -1);
  chomp;
}
Share:
14,342
dgBP
Author by

dgBP

Music loving techie who enjoys dabbling in a bit of this and that.

Updated on June 27, 2022

Comments

  • dgBP
    dgBP almost 2 years

    I have an array of strings, some of which contain the character '-'. I want to be able to search for it and for those strings that contain it I wish to delete all characters to the right of it.

    So for example if I have:

    $string1 = 'home - London';
    $string2 = 'office';
    $string3 = 'friend-Manchester';
    

    or something as such, then the affected strings would become:

    $string1 = 'home';
    $string3 = 'friend';
    

    I don't know if the white-space before the '-' would be included in the string afterwards (I don't want it as I will be comparing strings at a later point, although if it doesn't affect string comparisons then it doesn't matter).

    I do know that I can search and replace specific strings/characters using something like:

    $string1 =~ s/-//
    or 
    $string1 =~ tr/-//
    

    but I'm not very familiar with regular expressions in Perl so I'm not 100% sure of these. I've looked around and couldn't see anything to do with 'to the right of' in regex. Help appreciated!

  • dgBP
    dgBP over 11 years
    what exactly makes this look-ahead? Is it simply that you do not have the $string =~ stage?
  • snoofkin
    snoofkin over 11 years
    actually, I decided to change it to substr() and index() as this was not shown yet... [-: