110,539
社区成员
发帖
与我相关
我的任务
分享
string msg = "我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...";
//Regex.Replace(msg, @"");
string[] strNum = Regex.Split(msg, @"\.", RegexOptions.IgnorePatternWhitespace);
//Console.WriteLine(strNum);
string msg2 = null;
for (int i = 0; i < strNum.Length; i++)
{
msg2 += strNum;
}
msg2 = Regex.Replace(msg, @"(.)\1+", "$1");
Console.WriteLine(msg2);
Console.ReadKey();
string[] strNum = Regex.Split(msg, @"\.", RegexOptions.IgnorePatternWhitespace);
msg2 = Regex.Replace(msg, @"(.)\1+", "$1");
string.Join("", Regex.Matches("我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...", @"[\u4e00-\u9fa5]").Cast<Match>().Select(m => m.Value).Distinct())
//有字符串
//1_22_333,,4444__55555,_666666
//需要解析为
//1
//22
//333
//_
//4444
//55555
//666666
#include <stdio.h>
char s[]="1_22_333,,4444__55555,_666666";
char c,*p,*p1;
int st;
void main() {
st=0;
p=s;
while (1) {
c=*p;
if (0==c) {
switch (st) {
case 1:printf("_\n"); break;
case 2:printf("%s\n",p1);break;
}
break;//
}
switch (st) {
case 0:
if ('_'==c) { st=0;}
else if (','==c) { st=1;}
else {p1=p; st=2;}
break;
case 1:
if ('_'==c) { st=1;}
else if (','==c) {printf("_\n"); st=1;}
else {p1=p; st=2;}
break;
case 2:
if ('_'==c) {*p=0;printf("%s\n",p1);*p=c;st=0;}
else if (','==c) {*p=0;printf("%s\n",p1);*p=c;st=1;}
else { st=2;}
break;
}
p++;
}
}
//1
//22
//333
//_
//4444
//55555
//666666
提醒:正则表达式不是万能的;而有限状态自动机是万能的。static void Main()
{
string msg = "我...我我..我我我我....爱爱爱..爱..爱...你你...你..你你你...";
string pattern =
@"
(?<c> # 开启捕获组,名字叫c
[^.] # 匹配除'.'以外的任何一个字符
) # 结束捕获组
\.* # 任意个的'.'
( # 开启捕获组
\k<c> # 匹配上一个名字叫c的捕获组
\.* # 任意个的'.'
)* # 结束捕获组,并允许该组任意重复
";
string result = new Regex(pattern, RegexOptions.IgnorePatternWhitespace).Replace(msg, "${c}");
}