Reputation: 13
I need a Reg Ex script
example:
Upvotes: 1
Views: 227
Reputation: 4564
you could do something with several passes.
it's kind of generic workaround that could be shorted by using lookbehind.
(not all regex flavors do support this)
-
with regex -{2,}
-.
with regex [^-\.A-Za-z0-9]
.
with a temp character e.g. !
and replace remaining .
!
from last step with .
update using C# .net
(I'm not a C# programmer, used this regex tester and this reference for C# .net regex flavor.)
String str = "Mike&Ike ......";
str = Regex.Replace( str, @"-+", @"-" );
str = Regex.Replace( str, @"(?<=\.)(.*?)\.", @"$1" );
str = Regex.Replace( str, @"[^\w\r\n]", @"" );
-
with single -
.
if it's not the first .
using positiv lookbehind (?<=...)
\w
is short for [A-Za-z0-9]
Upvotes: 0
Reputation: 80384
#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;
my @samples = (
"Mike&Ike" => "MikeIke",
"Mike-Ike" => "Mike-Ike",
"Mike-Ike-Jill" => "Mike-Ike-Jill",
"Mike--Ike-Jill" => "Mike-Ike-Jill",
"Mike--Ike---Jill" => "Mike-Ike-Jill",
"Mike.Ike.Bill" => "Mike.IkeBill",
"Mike***Joe" => "MikeJoe",
"Mike123" => "Mike123",
);
while (my($got, $want) = splice(@samples, 0, 2)) {
my $had = $got;
for ($got) {
# 1) Allow max 1 dashy bit connected to each other.
s/ ( \p{Dash} ) \p{Dash}+ /$1/xg;
# 2) Allow max 1 period, total.
1 while s/ ^ [^.]* \. [^.]* \K \. //x ;
# 3) Remove all symbols...
s/ (?! [\p{Dash}.] ) [\p{Symbol}\p{Punctuation}] //xg ;
# ...and punctuation
# except for dashy bits and dots.
}
if ($got eq $want) { print "RIGHT" }
else { print "WRONG" }
print ":\thad\t<$had>\n\twanted\t<$want>\n\tgot\t<$got>\n";
}
Generates:
RIGHT: had <Mike&Ike>
wanted <MikeIke>
got <MikeIke>
RIGHT: had <Mike-Ike>
wanted <Mike-Ike>
got <Mike-Ike>
RIGHT: had <Mike-Ike-Jill>
wanted <Mike-Ike-Jill>
got <Mike-Ike-Jill>
RIGHT: had <Mike--Ike-Jill>
wanted <Mike-Ike-Jill>
got <Mike-Ike-Jill>
RIGHT: had <Mike--Ike---Jill>
wanted <Mike-Ike-Jill>
got <Mike-Ike-Jill>
RIGHT: had <Mike.Ike.Bill>
wanted <Mike.IkeBill>
got <Mike.IkeBill>
RIGHT: had <Mike***Joe>
wanted <MikeJoe>
got <MikeJoe>
RIGHT: had <Mike123>
wanted <Mike123>
got <Mike123>
Upvotes: 3