.net相当于php preg_replace

时间:2010-03-12 14:27:50

标签: c# php regex

php的preg_replace函数的c#.net等价物是什么?

php代码是这样的:

    const ALLOW_VALUES = '[^a-z0-9àáâäèéêëìíîïòóôöùûŵýÿyÁÂÄÈÉÊËÌÎÏÒÓÔÖÙÛÜŴYÝ]';

    public function streetTownHash($data, $hashCheck = false, $updateRecord = false)
     {
       foreach($data as $key=>$value){
        try{
         $value = mb_convert_case($value, MB_CASE_LOWER, "UTF-8");
         } catch(Exception $e) {
          echo "Requires extension=php_mbstring.dll enabled !  - $e";
         }
        $valueConcat .= preg_replace('/'.self::ALLOW_VALUES.'/','',$value); # Remove punctuation etc
       }
       $streetTownHash =  sha1($valueConcat);

....

编辑:现在就是这样。

    private readonly SHA1 hash = SHA1.Create();
    private readonly Regex PunctuationStripper = new Regex(@"[^a-z0-9àáâäèéêëìíîïòóôöùûŵýÿyÁÂÄÈÉÊËÌÎÏÒÓÔÖÙÛÜŴYÝ]", RegexOptions.IgnoreCase);
    public string HashString(string value)
    {
        value = value.ToLower();
        value = PunctuationStripper.Replace(value, string.Empty);
        var bytes = ASCIIEncoding.UTF8.GetBytes(value);
        var hashed = hash.ComputeHash(bytes);
        return ASCIIEncoding.UTF8.GetString(hashed);
    }

编辑:

正则表达式表达式是否也需要更改?

1 个答案:

答案 0 :(得分:1)

您正在寻找Regex课程。

例如:

static readonly Regex PunctuationStripper = new Regex(@"[^a-z0-9àáâäèéêëìíîïòóôöùûŵýÿyÁÂÄÈÉÊËÌÎÏÒÓÔÖÙÛÜŴYÝ]", RegexOptions.IgnoreCase);
//...
value = PunctuationStripper.Replace(value, "");