开发者

.net equivalent for php preg_replace

开发者 https://www.devze.com 2022-12-22 14:40 出处:网络
What is the c#.net equivalent for php\'s preg_replace function? php code is like this: const ALLOW_VALUES = \'[^a-z0-9àáâäèéêëìíîïòóôöùûŵýÿyÁÂÄÈÉÊËÌÎÏÒÓÔÖÙÛÜŴY

What is the c#.net equivalent for php's preg_replace function?

php code is like this:

    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);

....

Edit: now is like so.

    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开发者_Go百科, string.Empty);
        var bytes = ASCIIEncoding.UTF8.GetBytes(value);
        var hashed = hash.ComputeHash(bytes);
        return ASCIIEncoding.UTF8.GetString(hashed);
    }

Edit:

Would the regex expression need to change also?


You're looking for the Regex class.

For example:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号