简体   繁体   English

同时在同一个 Regex 上添加两个 Replace - 做两件不同的事情

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

My problem is that when I for example add a point more like it needs replacing.我的问题是,例如,当我添加一个更像是需要替换的点时。 Then it comes up with this error:然后它出现了这个错误:

cannot convert from string to System.stringComparison无法从字符串转换为 System.stringComparison

If I thus comment on the last replace.如果我因此评论最后一次替换。 Then there are no problems but this only happens if I have added the extra replace on my Regex.然后就没有问题了,但这只有在我在 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!!!");

I have also tried to do this:我也尝试过这样做:

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

if only Regex.Replace returns Regex we'll be able to chain Replace : Replace(...).Replace(...).Replace(...) ;如果只有Regex.Replace返回Regex我们将能够链接ReplaceReplace(...).Replace(...).Replace(...) but alas!可惜! the Replace returns string so we can't use Regex methods on it ( string ). Replace返回string因此我们不能对其使用Regex方法( string )。 The options are:选项是:

Nested calls:嵌套调用:

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

Sequential calls:顺序调用:

 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.

相关问题 相同的代码在两个不同的应用程序上做两个不同的事情? - Same code doing two different things on two different apps? 同时调用两个不同的控件 - Invoke two different controls at the same time 需要在两个不同的类中添加相同的属性 - Need to add same properties in two different classes 为什么相同的功能有2项不同的功能? - Why does the same function do 2 different things? 同时使用两个“ For” - Using Two “For” at the same time 在MVC控制器中,相同的string.replace()具有两种不同的含义 - same string.replace() given two different meanings in MVC controller 如何同时绑定到ComboBox中的两个不同的依赖项属性? - How do I bind to two different dependency properties in a ComboBox at the same time? 是否可以在两个 CPU 内核上同时执行两个 lock() 语句? CPU内核是否同时滴答作响? - Is it possible for two lock() statemens to be executed at the same time on two CPU cores ? Do CPU cores tick at the same time? Moq您如何(可以?)向同一个上下文中添加两个不同的(不兼容的)接口? - Moq How do you(Can you?) add two different (incompatible) interfaces to the same context? C#:使用不同的键同时移动两个矩形 - C#: Moving Two Rectangles At The Same Time With Different Keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM