小编Die*_*mos的帖子

用空格替换Unicode字符"�"

我正在从.csv文件中大量上传信息,我需要将此字符替换为ascii"�"以获取正常空间"".

对于C/C++/JAVA,字符"�"对应于"\ uFFFD",它似乎称为REPLACEMENT CHARACTER.其他如C#官方文档中的空格类型如U + FEFF,205F,200B,180E,202F.

我正在尝试以这种方式替换

public string Errors="";

public void test(){

    string textFromCsvCell= "";
    string validCharacters="^[0-9A-Za-z().:%-/ ]+$";
    textFromCsvCell="This is my text from csv file"; //ALl spaces aren't normal space " "
    string cleaned = textFromCsvCell.Replace("\uFFFD", "\"")     
      if (Regex.IsMatch(cleaned, validCharacters ))
        //All code for insert
      else
         Errors=cleaned;
         //print Errors
}
Run Code Online (Sandbox Code Playgroud)

测试方法给我看这个文字:

"这是来自csv文件的my�texto"

我尝试了一些解决方案

尝试解决方案1:使用修剪

 Regex.Replace(value.Trim(), @"[^\S\r\n]+", " ");
Run Code Online (Sandbox Code Playgroud)

尝试解决方案2:使用替换

  System.Text.RegularExpressions.Regex.Replace(str,@"\s+"," ");
Run Code Online (Sandbox Code Playgroud)

尝试解决方案3:使用修剪

  String.Trim(new char[]{'\uFEFF','\u200B'});
Run Code Online (Sandbox Code Playgroud)

尝试解决方案4:将[\ S\r \n]添加到validCharacters

  string validCharacters="^[\S\r\n0-9A-Za-z().:%-/ ]+$";
Run Code Online (Sandbox Code Playgroud)

什么都行不通

有人有想法吗?我怎样才能更换它?我非常感谢你的帮助,谢谢

资料来源:

http://www.fileformat.info/info/unicode/char/0fffd/index.htm

尝试用一个空格替换所有空白区域

从C#中的字符串剥离字节顺序标记

C#Regex - 删除额外的空格但保留新行 …

c# regex validation trim

6
推荐指数
1
解决办法
5945
查看次数

如何用正则表达式C#替换空格(将Unicode转换为utf-8)

我正在尝试在C#中执行替换正则表达式。我尝试编写的方法用UTF-8中的普通空格替换了一些Unicode字符(空格)。

让我用代码解释。我不好写正则表达式,文化信息和正则表达式。

    //This method replace white spaces in unicode by whitespaces UTF-8
    public static string cleanUnicodeSpaces(string value)
    {
        //This first pattern works but, remove other special characteres
        //For example: mark accents
        //string pattern = @"[^\u0000-\u007F]+"; 
        string cleaned = ""; 
        string pattern = @"[^\u0020\u0009\u000D]+"; //Unicode characters
        string replacement = ""; //Replace by UTF-8 space
        Regex regex = new Regex(pattern);
        cleaned = regex.Replace(value, replacement).Trim(); //Trim by quit spaces
        return cleaned;
    }
Run Code Online (Sandbox Code Playgroud)

Unicode空格

  • HT:U + 0009 =字符列表
  • LF:U + 000A =换行
  • CR:U + …

c# regex unicode replace

2
推荐指数
1
解决办法
885
查看次数

使用 Polly.Net 的嵌套重试和断路器策略的意外行为

我编写了基于重试的弹性策略和熔断策略。现在可以工作,但其行为存在问题。

我注意到,当断路器打开half-open并且onBreak()事件再次执行以关闭电路时,会为重试策略触发一次额外的重试(这是状态的另一项the health verificationhalf-open

让我一步步解释:

我定义了两个用于重试和断路器的强类型策略:

static Policy<HttpResponseMessage> customRetryPolicy;
static Policy<HttpResponseMessage> customCircuitBreakerPolicy;

static HttpStatusCode[] httpStatusesToProcess = new HttpStatusCode[]
{
   HttpStatusCode.ServiceUnavailable,  //503
   HttpStatusCode.InternalServerError, //500
};
Run Code Online (Sandbox Code Playgroud)

重试策略的工作方式如下:每个请求两次 (2) 重试,每次重试之间等待五 (5) 秒。如果内部断路器打开,不得重试。仅重试 500 和 503 Http 状态。

customRetryPolicy = Policy<HttpResponseMessage>   

//Not execute a retry if the circuit is open
.Handle<BrokenCircuitException>( x => 
{
    return !(x is BrokenCircuitException);
})

//Stop if some inner exception match with BrokenCircuitException
.OrInner<AggregateException>(x =>
{
    return !(x.InnerException is BrokenCircuitException);
})

//Retry …
Run Code Online (Sandbox Code Playgroud)

c# circuit-breaker .net-core polly asp.net-core

2
推荐指数
1
解决办法
3503
查看次数