首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式替换

正则表达式替换
EN

Stack Overflow用户
提问于 2011-03-28 18:23:03
回答 2查看 216关注 0票数 1

我需要一个Reg Ex脚本

  • 删除所有符号
  • 允许最大1连字符相互连接
  • 允许最大1期共计

示例:

  • Mike&Ike 输出是: MikeIke
  • 迈克-艾克的输出是:迈克-艾克
  • 迈克-艾克-吉尔的输出是:迈克-艾克-吉尔
  • Ike-Jill输出是: Mike-Ike-Jill
  • 迈克-艾克-吉尔输出是:迈克-艾克-吉尔
  • Mike.Ike.Bill 输出为: Mike.IkeBill
  • Joe输出是: MikeJoe
  • Mike123 输出为: Mike123
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-28 21:09:34

你可以通过几次传球就能做点什么。

这是一种通用的解决方法,可以通过使用lookbehind来缩短。

(并非所有regex口味都支持此功能)

  1. 用regex -删除多个-{2,}
  2. 删除符号,但-.和regex [^-\.A-Za-z0-9]除外
  3. 将第一个.替换为临时字符,例如!,并替换剩余的.
  4. 将最后一步的!替换为.

使用更新C# .net

(我不是C#程序员,我使用这个regex测试仪和这个参考文献来实现C# .net regex风格。)

代码语言:javascript
复制
String str = "Mike&Ike ......";
str = Regex.Replace( str, @"-+", @"-" );
str = Regex.Replace( str, @"(?<=\.)(.*?)\.", @"$1" );
str = Regex.Replace( str, @"[^\w\r\n]", @"" );
  1. 用单-代替multipe -
  2. 如果不是第一次使用正向后查找..,则删除(?<=...)
  3. 删除符号(实际上所有东西--不是单词、字符或换行符)-- \w[A-Za-z0-9]的缩写
票数 0
EN

Stack Overflow用户

发布于 2011-03-28 21:44:50

代码语言:javascript
复制
#!/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";
}

生成:

代码语言:javascript
复制
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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5463310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档