我需要一个Reg Ex脚本
示例:
发布于 2011-03-28 21:09:34
你可以通过几次传球就能做点什么。
这是一种通用的解决方法,可以通过使用lookbehind来缩短。
(并非所有regex口味都支持此功能)
-删除多个-{2,}-.和regex [^-\.A-Za-z0-9]除外.替换为临时字符,例如!,并替换剩余的.!替换为.使用更新C# .net
(我不是C#程序员,我使用这个regex测试仪和这个参考文献来实现C# .net regex风格。)
String str = "Mike&Ike ......";
str = Regex.Replace( str, @"-+", @"-" );
str = Regex.Replace( str, @"(?<=\.)(.*?)\.", @"$1" );
str = Regex.Replace( str, @"[^\w\r\n]", @"" );-代替multipe -.的.,则删除(?<=...)\w是[A-Za-z0-9]的缩写发布于 2011-03-28 21:44:50
#!/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>https://stackoverflow.com/questions/5463310
复制相似问题