我需要一个Reg Ex脚本
示例:
答案 0 :(得分:3)
#!/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";
}
生成:
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>
答案 1 :(得分:0)
你可以通过几次传球做点什么 它是一种通用的解决方法,可以通过使用lookbehind来缩短 (并非所有正则表达式都支持此功能)
-
-{2,}
-.
[^-\.A-Za-z0-9]
以外的符号
.
替换为临时字符,例如!
并替换剩余的.
!
.
醇>
更新 使用C#.net
(我不是C#程序员,使用此regex tester和此reference来表示C#.net正则表达式。)
String str = "Mike&Ike ......";
str = Regex.Replace( str, @"-+", @"-" );
str = Regex.Replace( str, @"(?<=\.)(.*?)\.", @"$1" );
str = Regex.Replace( str, @"[^\w\r\n]", @"" );
-
-
.
如果它不是第一个.
使用positiv lookbehind (?<=...)
\w
是[A-Za-z0-9]