Question

I have been pondering on this for quite awhile and still can't figure it out. The regex look ahead/behinds. Anyway, I'm not sure which to use in my situation, I am still having trouble grasping the concept. Let me give you an example.

I have a name....My Business,LLC (Milwaukee,WI)_12345678_12345678

What I want to do is if there is a comma in the name, no matter how many, remove it. At the same time, if there is not a comma in the name, still read the line. The one-liner I have is listed below.

s/(.*?)(_)(\d+_)(\d+$)/$1$2$3$4/gi;

I want to remove any comma from $1(My Business,LLC (Milwaukee,WI)). I could call out the comma in regex as a literal string((.?),(.?),(.*?)(_.*?$)) if it was this EXACT situation everytime, however it is not.

I want it to omit commas and match 'My Business, LLC_12345678_12345678' or just 'My Business_12345678_12345678', even though there is no comma. In any situation I want it to match the line, comma or not, and remove any commas(if any) no matter how many or where.

If someone can help me understand this concept, it will be a breakthrough!!

Was it helpful?

Solution

Use the /e modifier of Perl so that you can pass your function during the replace in s///

$str = 'My Business,LLC (Milwaukee,WI)_12345678_12345678';
## modified your regex as well using lookahead
$str =~ s/(.*?)(?=_\d+_\d+$)/funct($1)/ge;
print $str;

sub funct{
    my $val = shift;
    ## replacing , with empty, use anything what you want!
    $val =~ s/,//g;
    return $val;
}

Using funct($1) in substitute you are basically calling the funct() function with parameter $1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top