Can I use regular expressions with String.Replace in C#?











up vote
13
down vote

favorite












For example I have code below
string txt="I have strings like West, and West; and west, and Western."



I would like to replace the word west or West with some other word. But I would like not to replace West in Western.




  1. Can I use regular expression in string.replace? I used
    inputText.Replace("(\sWest.\s)",temp); It dos not work.










share|improve this question




























    up vote
    13
    down vote

    favorite












    For example I have code below
    string txt="I have strings like West, and West; and west, and Western."



    I would like to replace the word west or West with some other word. But I would like not to replace West in Western.




    1. Can I use regular expression in string.replace? I used
      inputText.Replace("(\sWest.\s)",temp); It dos not work.










    share|improve this question


























      up vote
      13
      down vote

      favorite









      up vote
      13
      down vote

      favorite











      For example I have code below
      string txt="I have strings like West, and West; and west, and Western."



      I would like to replace the word west or West with some other word. But I would like not to replace West in Western.




      1. Can I use regular expression in string.replace? I used
        inputText.Replace("(\sWest.\s)",temp); It dos not work.










      share|improve this question















      For example I have code below
      string txt="I have strings like West, and West; and west, and Western."



      I would like to replace the word west or West with some other word. But I would like not to replace West in Western.




      1. Can I use regular expression in string.replace? I used
        inputText.Replace("(\sWest.\s)",temp); It dos not work.







      c# string text matching






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 5 '10 at 6:44









      Roger Lipscombe

      55.2k42185309




      55.2k42185309










      asked May 5 '10 at 6:30









      Tasawer Khan

      3,37463464




      3,37463464
























          9 Answers
          9






          active

          oldest

          votes

















          up vote
          20
          down vote



          accepted










          To replace the whole word (rather than part of the word):



          string s = Regex.Replace(s, @"bwestb", "something");





          share|improve this answer





















          • Looks alright but this will ignore west, and west. And is it case insensitive?
            – Tasawer Khan
            May 5 '10 at 6:44










          • I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
            – Tasawer Khan
            May 5 '10 at 6:50










          • It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
            – Tasawer Khan
            May 5 '10 at 7:06










          • The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
            – Hans Kesting
            May 6 '10 at 7:27


















          up vote
          23
          down vote













          Answer to the question is NO - you cannot use regexp in string.Replace.



          If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.






          share|improve this answer

















          • 4




            +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
            – drkthng
            Oct 6 '15 at 9:37


















          up vote
          7
          down vote













          Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.






          share|improve this answer




























            up vote
            4
            down vote













            Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like



            string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);


            should work, if your regular expression is correct.






            share|improve this answer




























              up vote
              2
              down vote













              USe this code if you want it to be case insensitive



              string pattern = @"bwestb";
              string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);





              share|improve this answer






























                up vote
                1
                down vote













                I agree with Robert Harvey's solution except for one small modification:



                s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);


                This will replace both "West" and "west" with your new word






                share|improve this answer




























                  up vote
                  0
                  down vote













                  Insert the regular expression in the code before class



                  using System.Text.RegularExpressions;


                  below is the code for string replace using regex



                  string input = "Dot > Not Perls";
                  // Use Regex.Replace to replace the pattern in the input.
                  string output = Regex.Replace(input, "some string", ">");


                  source : http://www.dotnetperls.com/regex-replace






                  share|improve this answer




























                    up vote
                    0
                    down vote













                    In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:



                    public static string ReplaceX(this string text, string regex, string replacement) {
                    return Regex.Replace(text, regex, replacement);
                    }


                    And use it like:



                    var text = "      space          more spaces  ";
                    text.Trim().ReplaceX(@"s+", " "); // "space more spaces"





                    share|improve this answer




























                      up vote
                      -1
                      down vote













                      class Regex is static, when You use the method Replace (Regex.Replace).



                      public static class Extension
                      {
                      public static string ReplaceValue(string data,string criteria)
                      {
                      return s = Regex.Replace(s, @"bwestb", "something");
                      }
                      }





                      share|improve this answer



















                      • 1




                        This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                        – David Makogon
                        Jul 21 '16 at 22:10











                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                       

                      draft saved


                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2771042%2fcan-i-use-regular-expressions-with-string-replace-in-c%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      9 Answers
                      9






                      active

                      oldest

                      votes








                      9 Answers
                      9






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      20
                      down vote



                      accepted










                      To replace the whole word (rather than part of the word):



                      string s = Regex.Replace(s, @"bwestb", "something");





                      share|improve this answer





















                      • Looks alright but this will ignore west, and west. And is it case insensitive?
                        – Tasawer Khan
                        May 5 '10 at 6:44










                      • I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                        – Tasawer Khan
                        May 5 '10 at 6:50










                      • It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                        – Tasawer Khan
                        May 5 '10 at 7:06










                      • The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                        – Hans Kesting
                        May 6 '10 at 7:27















                      up vote
                      20
                      down vote



                      accepted










                      To replace the whole word (rather than part of the word):



                      string s = Regex.Replace(s, @"bwestb", "something");





                      share|improve this answer





















                      • Looks alright but this will ignore west, and west. And is it case insensitive?
                        – Tasawer Khan
                        May 5 '10 at 6:44










                      • I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                        – Tasawer Khan
                        May 5 '10 at 6:50










                      • It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                        – Tasawer Khan
                        May 5 '10 at 7:06










                      • The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                        – Hans Kesting
                        May 6 '10 at 7:27













                      up vote
                      20
                      down vote



                      accepted







                      up vote
                      20
                      down vote



                      accepted






                      To replace the whole word (rather than part of the word):



                      string s = Regex.Replace(s, @"bwestb", "something");





                      share|improve this answer












                      To replace the whole word (rather than part of the word):



                      string s = Regex.Replace(s, @"bwestb", "something");






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 5 '10 at 6:35









                      Robert Harvey

                      147k33270413




                      147k33270413












                      • Looks alright but this will ignore west, and west. And is it case insensitive?
                        – Tasawer Khan
                        May 5 '10 at 6:44










                      • I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                        – Tasawer Khan
                        May 5 '10 at 6:50










                      • It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                        – Tasawer Khan
                        May 5 '10 at 7:06










                      • The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                        – Hans Kesting
                        May 6 '10 at 7:27


















                      • Looks alright but this will ignore west, and west. And is it case insensitive?
                        – Tasawer Khan
                        May 5 '10 at 6:44










                      • I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                        – Tasawer Khan
                        May 5 '10 at 6:50










                      • It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                        – Tasawer Khan
                        May 5 '10 at 7:06










                      • The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                        – Hans Kesting
                        May 6 '10 at 7:27
















                      Looks alright but this will ignore west, and west. And is it case insensitive?
                      – Tasawer Khan
                      May 5 '10 at 6:44




                      Looks alright but this will ignore west, and west. And is it case insensitive?
                      – Tasawer Khan
                      May 5 '10 at 6:44












                      I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                      – Tasawer Khan
                      May 5 '10 at 6:50




                      I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
                      – Tasawer Khan
                      May 5 '10 at 6:50












                      It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                      – Tasawer Khan
                      May 5 '10 at 7:06




                      It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
                      – Tasawer Khan
                      May 5 '10 at 7:06












                      The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                      – Hans Kesting
                      May 6 '10 at 7:27




                      The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
                      – Hans Kesting
                      May 6 '10 at 7:27












                      up vote
                      23
                      down vote













                      Answer to the question is NO - you cannot use regexp in string.Replace.



                      If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.






                      share|improve this answer

















                      • 4




                        +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                        – drkthng
                        Oct 6 '15 at 9:37















                      up vote
                      23
                      down vote













                      Answer to the question is NO - you cannot use regexp in string.Replace.



                      If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.






                      share|improve this answer

















                      • 4




                        +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                        – drkthng
                        Oct 6 '15 at 9:37













                      up vote
                      23
                      down vote










                      up vote
                      23
                      down vote









                      Answer to the question is NO - you cannot use regexp in string.Replace.



                      If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.






                      share|improve this answer












                      Answer to the question is NO - you cannot use regexp in string.Replace.



                      If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 21 '15 at 9:44









                      dortique

                      50049




                      50049








                      • 4




                        +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                        – drkthng
                        Oct 6 '15 at 9:37














                      • 4




                        +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                        – drkthng
                        Oct 6 '15 at 9:37








                      4




                      4




                      +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                      – drkthng
                      Oct 6 '15 at 9:37




                      +1 only answer that directly answers the question if you can use String.Replace also for regex expressions
                      – drkthng
                      Oct 6 '15 at 9:37










                      up vote
                      7
                      down vote













                      Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.






                      share|improve this answer

























                        up vote
                        7
                        down vote













                        Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.






                        share|improve this answer























                          up vote
                          7
                          down vote










                          up vote
                          7
                          down vote









                          Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.






                          share|improve this answer












                          Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 5 '10 at 6:36









                          Marc Gravell

                          770k19021172529




                          770k19021172529






















                              up vote
                              4
                              down vote













                              Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like



                              string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);


                              should work, if your regular expression is correct.






                              share|improve this answer

























                                up vote
                                4
                                down vote













                                Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like



                                string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);


                                should work, if your regular expression is correct.






                                share|improve this answer























                                  up vote
                                  4
                                  down vote










                                  up vote
                                  4
                                  down vote









                                  Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like



                                  string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);


                                  should work, if your regular expression is correct.






                                  share|improve this answer












                                  Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like



                                  string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);


                                  should work, if your regular expression is correct.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered May 5 '10 at 6:40









                                  Peter

                                  1,3721118




                                  1,3721118






















                                      up vote
                                      2
                                      down vote













                                      USe this code if you want it to be case insensitive



                                      string pattern = @"bwestb";
                                      string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);





                                      share|improve this answer



























                                        up vote
                                        2
                                        down vote













                                        USe this code if you want it to be case insensitive



                                        string pattern = @"bwestb";
                                        string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);





                                        share|improve this answer

























                                          up vote
                                          2
                                          down vote










                                          up vote
                                          2
                                          down vote









                                          USe this code if you want it to be case insensitive



                                          string pattern = @"bwestb";
                                          string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);





                                          share|improve this answer














                                          USe this code if you want it to be case insensitive



                                          string pattern = @"bwestb";
                                          string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 5 '10 at 6:57

























                                          answered May 5 '10 at 6:50









                                          Archie

                                          1,30683150




                                          1,30683150






















                                              up vote
                                              1
                                              down vote













                                              I agree with Robert Harvey's solution except for one small modification:



                                              s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);


                                              This will replace both "West" and "west" with your new word






                                              share|improve this answer

























                                                up vote
                                                1
                                                down vote













                                                I agree with Robert Harvey's solution except for one small modification:



                                                s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);


                                                This will replace both "West" and "west" with your new word






                                                share|improve this answer























                                                  up vote
                                                  1
                                                  down vote










                                                  up vote
                                                  1
                                                  down vote









                                                  I agree with Robert Harvey's solution except for one small modification:



                                                  s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);


                                                  This will replace both "West" and "west" with your new word






                                                  share|improve this answer












                                                  I agree with Robert Harvey's solution except for one small modification:



                                                  s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);


                                                  This will replace both "West" and "west" with your new word







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered May 5 '10 at 7:23









                                                  Kyllan

                                                  63210




                                                  63210






















                                                      up vote
                                                      0
                                                      down vote













                                                      Insert the regular expression in the code before class



                                                      using System.Text.RegularExpressions;


                                                      below is the code for string replace using regex



                                                      string input = "Dot > Not Perls";
                                                      // Use Regex.Replace to replace the pattern in the input.
                                                      string output = Regex.Replace(input, "some string", ">");


                                                      source : http://www.dotnetperls.com/regex-replace






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        Insert the regular expression in the code before class



                                                        using System.Text.RegularExpressions;


                                                        below is the code for string replace using regex



                                                        string input = "Dot > Not Perls";
                                                        // Use Regex.Replace to replace the pattern in the input.
                                                        string output = Regex.Replace(input, "some string", ">");


                                                        source : http://www.dotnetperls.com/regex-replace






                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          Insert the regular expression in the code before class



                                                          using System.Text.RegularExpressions;


                                                          below is the code for string replace using regex



                                                          string input = "Dot > Not Perls";
                                                          // Use Regex.Replace to replace the pattern in the input.
                                                          string output = Regex.Replace(input, "some string", ">");


                                                          source : http://www.dotnetperls.com/regex-replace






                                                          share|improve this answer












                                                          Insert the regular expression in the code before class



                                                          using System.Text.RegularExpressions;


                                                          below is the code for string replace using regex



                                                          string input = "Dot > Not Perls";
                                                          // Use Regex.Replace to replace the pattern in the input.
                                                          string output = Regex.Replace(input, "some string", ">");


                                                          source : http://www.dotnetperls.com/regex-replace







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Oct 1 '14 at 13:01









                                                          Talha

                                                          35




                                                          35






















                                                              up vote
                                                              0
                                                              down vote













                                                              In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:



                                                              public static string ReplaceX(this string text, string regex, string replacement) {
                                                              return Regex.Replace(text, regex, replacement);
                                                              }


                                                              And use it like:



                                                              var text = "      space          more spaces  ";
                                                              text.Trim().ReplaceX(@"s+", " "); // "space more spaces"





                                                              share|improve this answer

























                                                                up vote
                                                                0
                                                                down vote













                                                                In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:



                                                                public static string ReplaceX(this string text, string regex, string replacement) {
                                                                return Regex.Replace(text, regex, replacement);
                                                                }


                                                                And use it like:



                                                                var text = "      space          more spaces  ";
                                                                text.Trim().ReplaceX(@"s+", " "); // "space more spaces"





                                                                share|improve this answer























                                                                  up vote
                                                                  0
                                                                  down vote










                                                                  up vote
                                                                  0
                                                                  down vote









                                                                  In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:



                                                                  public static string ReplaceX(this string text, string regex, string replacement) {
                                                                  return Regex.Replace(text, regex, replacement);
                                                                  }


                                                                  And use it like:



                                                                  var text = "      space          more spaces  ";
                                                                  text.Trim().ReplaceX(@"s+", " "); // "space more spaces"





                                                                  share|improve this answer












                                                                  In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:



                                                                  public static string ReplaceX(this string text, string regex, string replacement) {
                                                                  return Regex.Replace(text, regex, replacement);
                                                                  }


                                                                  And use it like:



                                                                  var text = "      space          more spaces  ";
                                                                  text.Trim().ReplaceX(@"s+", " "); // "space more spaces"






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered May 17 '17 at 6:21









                                                                  mr5

                                                                  1,55522546




                                                                  1,55522546






















                                                                      up vote
                                                                      -1
                                                                      down vote













                                                                      class Regex is static, when You use the method Replace (Regex.Replace).



                                                                      public static class Extension
                                                                      {
                                                                      public static string ReplaceValue(string data,string criteria)
                                                                      {
                                                                      return s = Regex.Replace(s, @"bwestb", "something");
                                                                      }
                                                                      }





                                                                      share|improve this answer



















                                                                      • 1




                                                                        This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                        – David Makogon
                                                                        Jul 21 '16 at 22:10















                                                                      up vote
                                                                      -1
                                                                      down vote













                                                                      class Regex is static, when You use the method Replace (Regex.Replace).



                                                                      public static class Extension
                                                                      {
                                                                      public static string ReplaceValue(string data,string criteria)
                                                                      {
                                                                      return s = Regex.Replace(s, @"bwestb", "something");
                                                                      }
                                                                      }





                                                                      share|improve this answer



















                                                                      • 1




                                                                        This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                        – David Makogon
                                                                        Jul 21 '16 at 22:10













                                                                      up vote
                                                                      -1
                                                                      down vote










                                                                      up vote
                                                                      -1
                                                                      down vote









                                                                      class Regex is static, when You use the method Replace (Regex.Replace).



                                                                      public static class Extension
                                                                      {
                                                                      public static string ReplaceValue(string data,string criteria)
                                                                      {
                                                                      return s = Regex.Replace(s, @"bwestb", "something");
                                                                      }
                                                                      }





                                                                      share|improve this answer














                                                                      class Regex is static, when You use the method Replace (Regex.Replace).



                                                                      public static class Extension
                                                                      {
                                                                      public static string ReplaceValue(string data,string criteria)
                                                                      {
                                                                      return s = Regex.Replace(s, @"bwestb", "something");
                                                                      }
                                                                      }






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Jul 21 '16 at 22:24

























                                                                      answered Jul 21 '16 at 21:46









                                                                      slnit

                                                                      185




                                                                      185








                                                                      • 1




                                                                        This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                        – David Makogon
                                                                        Jul 21 '16 at 22:10














                                                                      • 1




                                                                        This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                        – David Makogon
                                                                        Jul 21 '16 at 22:10








                                                                      1




                                                                      1




                                                                      This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                      – David Makogon
                                                                      Jul 21 '16 at 22:10




                                                                      This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
                                                                      – David Makogon
                                                                      Jul 21 '16 at 22:10


















                                                                       

                                                                      draft saved


                                                                      draft discarded



















































                                                                       


                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2771042%2fcan-i-use-regular-expressions-with-string-replace-in-c%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Berounka

                                                                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

                                                                      Sphinx de Gizeh