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, "");
精彩评论