簡體   English   中英

同時在同一個 Regex 上添加兩個 Replace - 做兩件不同的事情

[英]Add two Replace on the same Regex at the same time - Do two different things

我的問題是,例如,當我添加一個更像是需要替換的點時。 然后它出現了這個錯誤:

無法從字符串轉換為 System.stringComparison

如果我因此評論最后一次替換。 然后就沒有問題了,但這只有在我在 Regex 上添加了額外的替換時才會發生。

text = Regex.Replace(text, @"{(?s)(.*){medlem}}.*{{medlemstop}}",
       "<img src=\"https://aaaa.azureedge.net/imagesfiles/hello.png\" class=\"img-responsive\" alt=\"hello world\">")
       .Replace(text, @"{(?s)(.*){pay}}.*{{paystop}}", "ERROR HERE!!!");

我也嘗試過這樣做:

https://stackoverflow.com/a/6276014/12596984

如果只有Regex.Replace返回Regex我們將能夠鏈接ReplaceReplace(...).Replace(...).Replace(...) 可惜! Replace返回string因此我們不能對其使用Regex方法( string )。 選項是:

嵌套調用:

text = Regex.Replace(
         Regex.Replace(
                 text, 
               @"{(?s)(.*){medlem}}.*{{medlemstop}}",
                "<img src=\"https://aaaa.azureedge.net/imagesfiles/hello.png\" class=\"img-responsive\" alt=\"hello world\">"),
         @"{(?s)(.*){pay}}.*{{paystop}}", 
          "ERROR HERE!!!");

順序調用:

 text = Regex.Replace(
              text, 
            @"{(?s)(.*){medlem}}.*{{medlemstop}}",
             "<img src=\"https://aaaa.azureedge.net/imagesfiles/hello.png\" class=\"img-responsive\" alt=\"hello world\">");

 text = Regex.Replace(
             text, 
            @"{(?s)(.*){pay}}.*{{paystop}}", 
             "ERROR HERE!!!");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM