【问题标题】:How to delete repeated specific characters如何删除重复的特定字符
【发布时间】:2016-09-02 08:36:51
【问题描述】:

我需要将字符串中的“@”替换为“-”。这很简单,但我还需要用一个“-”替换多个“@@@@@”。关于如何用 ASP 做后者的任何想法。 这是一个例子:

输入字符串: @Introducción a los Esquemas Algorítmicos:Apuntes y colección de problemas。报告 LSI-97-6-T@@@@@@@@09/30/1997@@@@@TRE@

期望的输出: -Introducción a los Esquemas Algorítmicos: Apuntes y colección de problemas。报告 LSI-97-6-T-09/30/1997-TRE-

谢谢。

【问题讨论】:

  • 你能举个例子说明目前对你没有用的东西吗?
  • 旧的 ASP 或 ASP.NET?
  • 那么问题是,如果我这样做: Replace(string,"@","-") 我会重复“-”,当多个“@”时我只想要一个“-”在输入字符串中找到。谢谢
  • 您也只是指示将单个“@”替换为单个“-”。您需要查看正则表达式量词。 regular-expressions.info/repeat.html
  • 实际上,DNKROZ 发布了 2 个答案并使用 @+ 正则表达式将其删除。我相信last one 可以做到。

标签: regex string replace character


【解决方案1】:

试试这个经典的 ASP:

Dim regEx
Set regEx = New RegExp

 With regEx
    .Pattern = "([\@])\1+|(\@)"
    .Global = True
    .MultiLine = True
End With
strMessage = regEx.Replace(str, "-")

这将匹配每个出现的多个@@@@ 或单个出现的@

不确定您使用的是什么语言,所以这里是带有分隔符的完整表达式:/([\@])\1+|(\@)/g

编辑 - 更简单:/@+/g

【讨论】:

  • 没问题,很高兴为您提供帮助。请尽可能接受作为答案:)
  • 在 ASP 正则表达式引擎中有什么特别的地方不允许你只使用@+
  • 不——你是对的。我最初确实使用@+ 发布了一个答案,但是当我认为 OP 正在寻找不同的东西时,我将其删除了。然而,它们确实产生了相同的结果。我会更新我的答案
【解决方案2】:
    using System;
    using System.Text.RegularExpressions;

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World");
            String input = "@Introducción a los Esquemas Algorítmicos: Apuntes y colección de problemas. Report LSI-97-6-T@@@@@@@@09/30/1997@@@@@TRE@";
            String output=Regex.Replace(input,@"\@+","-");
            Console.WriteLine(output);

        }
    }

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 2014-04-08
    • 2012-10-21
    • 1970-01-01
    • 2012-03-08
    • 2016-08-30
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多