C#preg_replace?

时间:2020-03-06 15:04:30  来源:igfitidea点击:

什么是C#中的PHP preg_replace?

我有一个字符串数组,我想用另一个字符串数组替换。这是PHP中的示例。我如何在不使用.Replace(" old"," new")的情况下在C中做类似的事情。

$patterns[0] = '/=C0/';
$patterns[1] = '/=E9/';
$patterns[2] = '/=C9/';

$replacements[0] = 'à';
$replacements[1] = 'é';
$replacements[2] = 'é';
return preg_replace($patterns, $replacements, $text);

解决方案

我们正在寻找System.Text.RegularExpressions;

using System.Text.RegularExpressions;

Regex r = new Regex("=C0");
string output = r.Replace(text);

要以方式获得PHP的数组行为,我们需要多个Regex实例

但是,在示例中,使用.Replace(旧的,新的)会更好,它比编译状态机要快得多。

真正的男人使用正则表达式,但是如果需要,可以使用以下扩展方法将其添加到String中:

public static class ExtensionMethods
{
    public static String PregReplace(this String input, string[] pattern, string[] replacements)
    {
        if (replacements.Length != pattern.Length)
            throw new ArgumentException("Replacement and Pattern Arrays must be balanced");

        for (var i = 0; i < pattern.Length; i++)
        {
            input = Regex.Replace(input, pattern[i], replacements[i]);                
        }

        return input;
    }
}

我们可以这样使用它:

class Program
    {
        static void Main(string[] args)
        {
            String[] pattern = new String[4];
            String[] replacement = new String[4];

            pattern[0] = "Quick";
            pattern[1] = "Fox";
            pattern[2] = "Jumped";
            pattern[3] = "Lazy";

            replacement[0] = "Slow";            
            replacement[1] = "Turtle";
            replacement[2] = "Crawled";
            replacement[3] = "Dead";

            String DemoText = "The Quick Brown Fox Jumped Over the Lazy Dog";

            Console.WriteLine(DemoText.PregReplace(pattern, replacement));
        }        
    }

我们可以使用.Select()(在.NET 3.5和C3中)来简化将函数应用于集合成员的过程。

stringsList.Select( s => replacementsList.Select( r => s.Replace(s,r) ) );

我们不需要正则表达式支持,只需要一种简单的方法即可遍历数组。

编辑:呃,我刚刚意识到这个问题是针对2.0的,但是如果我们确实可以访问3.5,我将保留它。

这是Linq的另一件事。现在,我使用List <Char>代替Char [],但这只是为了使其看起来更整洁。数组上没有IndexOf方法,但列表上有一个方法。我为什么需要这个?根据我的猜测,替换列表与要替换的列表之间没有直接关联。只是索引。

因此,考虑到这一点,我们可以使用Char []来完成此操作。但是,当我们看到IndexOf方法时,必须在它之前添加一个.ToList()。

像这样:someArray.ToList()。IndexOf

String text;
  List<Char> patternsToReplace;
  List<Char> patternsToUse;

  patternsToReplace = new List<Char>();
  patternsToReplace.Add('a');
  patternsToReplace.Add('c');
  patternsToUse = new List<Char>();
  patternsToUse.Add('X');
  patternsToUse.Add('Z');

  text = "This is a thing to replace stuff with";

  var allAsAndCs = text.ToCharArray()
                 .Select
                 (
                   currentItem => patternsToReplace.Contains(currentItem) 
                     ? patternsToUse[patternsToReplace.IndexOf(currentItem)] 
                     : currentItem
                 )
                 .ToArray();

  text = new String(allAsAndCs);

这只是将文本转换为字符数组,并逐一选择。如果当前字符不在替换列表中,请照原样发送回该字符。如果它在替换列表中,则返回与替换字符列表相同索引中的字符。最后一件事是从字符数组创建一个字符串。

using System;
  using System.Collections.Generic;
  using System.Linq;

public static class StringManipulation
{
    public static string PregReplace(string input, string[] pattern, string[] replacements)
    {
        if (replacements.Length != pattern.Length)
            throw new ArgumentException("Replacement and Pattern Arrays must be balanced");

        for (int i = 0; i < pattern.Length; i++)
        {
            input = Regex.Replace(input, pattern[i], replacements[i]);                
        }

        return input;
    }
}

这是我将要使用的。 Jonathan Holland的一些代码,但不是在C#3.5中,而是在C#2.0中:)

谢谢。