Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya Monks,

I need your help in the following o get the output as mentioned for name1 output should be output1 and for name2 output should be output2. Pls suggest me a regex for this.

$name1 = 'Morrisville Jones-Ingham Executive and Sales Recruiting '; $name2 = 'Jones-Ingham Executive and Sales Recruiting - Morrisville, N +C, United States of America'; $output1 = 'Morrisville Jones-Ingham Executive and Sales Recruiting'; $output2 = 'Jones-Ingham Executive and Sales Recruiting';

"Morrisville" is the location. Basically, I dont want to delete anything if the location comes in front(as $name1), but if the location comes in middle($name2) I want to delete that location following with deleting whatever follows it. Can any monks pls suggest me a common regex for this in refer to 'Morrisville' as $location?

Replies are listed 'Best First'.
Re: Regex Help!
by choroba (Cardinal) on Apr 18, 2013 at 07:12 UTC
    The following two regexes transform the given names into the given outputs.
    s/[^[:alpha:]]+Morrisville.*//; s/ *$//;

    The first one removes Morrisville and the following together with any non-alphanumericbetic characters preceding it provided such characters exist. The second one removes the space at the end of the first name.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Why does it remove the standalone - but not the hyphen in Jones-Ingram?

        I beg your pardon? It removes the - because it is not alphabetic and is followed by the location. The hyphen in Jones-Ingram is not followed by the location.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regex Help!
by hdb (Monsignor) on Apr 18, 2013 at 07:01 UTC

    See below. The regex assumes that you have a - in front of the location. If this is not necessarily the case you need to remove it from the regex. But then Mr Morrisville will create problems...

    $name1 = 'Morrisville Jones-Ingham Executive and Sales Recruiting '; $name2 = 'Jones-Ingham Executive and Sales Recruiting - Morrisville, N +C, United States of America'; $location = "Morrisville"; $name1 =~ s/ - $location.*//; print $name1,"\n"; $name2 =~ s/ - $location.*//; print $name2,"\n";

      The regex below deletes the location and everything following if it is not at the start of the string. It does not remove the -. A single space at the start would suffice though to get the location at the beginning removed.

      $name1 =~ s/(.+)$location.*/$1/;