Regex to match backslash inside a string












1















I'm trying to match the following strings:




  • thistest_

  • _thistest

  • _thistest


In other words, the allowed strings have ONLY a backslash, splitting 2 substrings which can contain numbers, letters and _ characters.



I tried the following regex, testing it on http://regexhero.net/tester/:
^[a-zA-Z_][\]?[a-zA-Z0-9_]+$



Unfortunately, it recognizes also the following not allowed strings:




  • this\

  • _

  • _wsx


Any help please?










share|improve this question





























    1















    I'm trying to match the following strings:




    • thistest_

    • _thistest

    • _thistest


    In other words, the allowed strings have ONLY a backslash, splitting 2 substrings which can contain numbers, letters and _ characters.



    I tried the following regex, testing it on http://regexhero.net/tester/:
    ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$



    Unfortunately, it recognizes also the following not allowed strings:




    • this\

    • _

    • _wsx


    Any help please?










    share|improve this question



























      1












      1








      1








      I'm trying to match the following strings:




      • thistest_

      • _thistest

      • _thistest


      In other words, the allowed strings have ONLY a backslash, splitting 2 substrings which can contain numbers, letters and _ characters.



      I tried the following regex, testing it on http://regexhero.net/tester/:
      ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$



      Unfortunately, it recognizes also the following not allowed strings:




      • this\

      • _

      • _wsx


      Any help please?










      share|improve this question
















      I'm trying to match the following strings:




      • thistest_

      • _thistest

      • _thistest


      In other words, the allowed strings have ONLY a backslash, splitting 2 substrings which can contain numbers, letters and _ characters.



      I tried the following regex, testing it on http://regexhero.net/tester/:
      ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$



      Unfortunately, it recognizes also the following not allowed strings:




      • this\

      • _

      • _wsx


      Any help please?







      c# regex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 2 '14 at 10:34









      Chris

      23.7k36082




      23.7k36082










      asked Oct 2 '14 at 10:25









      ZanziZanzi

      2218




      2218
























          4 Answers
          4






          active

          oldest

          votes


















          0














          Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.



          Using:



          "^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"


          Will not match any of your test examples, since this will match, in order:





          • ^ beginning of string,


          • [a-zA-Z_] 1 alpha character or underscore,


          • [\]? 1 optional backslash,


          • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,


          • $ end of string


          If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:



          @"^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"




          • ^ beginning of string,


          • [a-zA-Z_] 1 alpha character or underscore,


          • [\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,


          • $ end of string.




          So what you actually need is either:



          "^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"


          (Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)



          Or



          @"^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$"


          (No backslash substitution performed, so the regex engine directly interprets the escaped backslash)



          Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.






          share|improve this answer
























          • Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

            – Zanzi
            Oct 2 '14 at 12:08













          • Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

            – Zanzi
            Oct 2 '14 at 12:16











          • @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

            – Jerry
            Oct 2 '14 at 12:26













          • Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

            – Zanzi
            Oct 2 '14 at 12:34











          • @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

            – Jerry
            Oct 2 '14 at 13:06



















          1














          Don't make the as optional. The below regex won't allow two or more backslashes and asserts that there must be atleast one word character present before and after to the symbol.



          @"^w+\w+$"


          OR



          @"^[A-Za-z0-9_]+\[A-Za-z0-9_]+$"


          DEMO






          share|improve this answer





















          • 1





            errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

            – Chris
            Oct 2 '14 at 10:50













          • You added the rawing after reading my answer =/

            – Jerry
            Oct 2 '14 at 11:04











          • I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

            – Avinash Raj
            Oct 2 '14 at 11:05













          • see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

            – Avinash Raj
            Oct 2 '14 at 11:09













          • @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

            – Jerry
            Oct 2 '14 at 11:21



















          0














          The best way to fix up your regex is the following:



          ^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$



          This breaks down to:



          NODE                     EXPLANATION
          --------------------------------------------------------------------------------
          ^ the beginning of the string
          --------------------------------------------------------------------------------
          [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
          '0' to '9', '_' (1 or more times (matching
          the most amount possible))
          --------------------------------------------------------------------------------
          \ ''
          --------------------------------------------------------------------------------
          [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
          '0' to '9', '_' (1 or more times (matching
          the most amount possible))
          --------------------------------------------------------------------------------
          $ before an optional n, and the end of the
          string


          Explanation courtesy of http://rick.measham.id.au/paste/explain.pl



          As you can see we have the same pattern before and after the backslash (since you indicated they should both be letters, numbers and underscores) with the + modifier meaning at least one. Then in the middle there is just the backslash which is compulsory.



          Since it is unclear whether when you said "letters" you meant the basic alphabet or if you meant anything that is letter like (most obviously accented characters but also any other alphabet, etc.) then you may want to expand your set of characters by using something like w as Avinash Raj suggests. See http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#WordCharacter for more info on what the "word character" covers.






          share|improve this answer































            0














            Pretty sure this should work if i understood everything you wanted.



            ^([a-zA-Z0-9_]+\[a-zA-Z0-9_]+)





            share|improve this answer


























            • You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

              – Chris
              Oct 2 '14 at 11:02











            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',
            autoActivateHeartbeat: false,
            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%2f26158883%2fregex-to-match-backslash-inside-a-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.



            Using:



            "^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"


            Will not match any of your test examples, since this will match, in order:





            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]? 1 optional backslash,


            • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,


            • $ end of string


            If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:



            @"^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"




            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,


            • $ end of string.




            So what you actually need is either:



            "^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"


            (Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)



            Or



            @"^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$"


            (No backslash substitution performed, so the regex engine directly interprets the escaped backslash)



            Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.






            share|improve this answer
























            • Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

              – Zanzi
              Oct 2 '14 at 12:08













            • Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

              – Zanzi
              Oct 2 '14 at 12:16











            • @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

              – Jerry
              Oct 2 '14 at 12:26













            • Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

              – Zanzi
              Oct 2 '14 at 12:34











            • @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

              – Jerry
              Oct 2 '14 at 13:06
















            0














            Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.



            Using:



            "^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"


            Will not match any of your test examples, since this will match, in order:





            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]? 1 optional backslash,


            • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,


            • $ end of string


            If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:



            @"^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"




            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,


            • $ end of string.




            So what you actually need is either:



            "^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"


            (Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)



            Or



            @"^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$"


            (No backslash substitution performed, so the regex engine directly interprets the escaped backslash)



            Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.






            share|improve this answer
























            • Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

              – Zanzi
              Oct 2 '14 at 12:08













            • Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

              – Zanzi
              Oct 2 '14 at 12:16











            • @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

              – Jerry
              Oct 2 '14 at 12:26













            • Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

              – Zanzi
              Oct 2 '14 at 12:34











            • @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

              – Jerry
              Oct 2 '14 at 13:06














            0












            0








            0







            Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.



            Using:



            "^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"


            Will not match any of your test examples, since this will match, in order:





            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]? 1 optional backslash,


            • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,


            • $ end of string


            If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:



            @"^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"




            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,


            • $ end of string.




            So what you actually need is either:



            "^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"


            (Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)



            Or



            @"^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$"


            (No backslash substitution performed, so the regex engine directly interprets the escaped backslash)



            Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.






            share|improve this answer













            Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.



            Using:



            "^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"


            Will not match any of your test examples, since this will match, in order:





            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]? 1 optional backslash,


            • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,


            • $ end of string


            If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:



            @"^[a-zA-Z_][\]?[a-zA-Z0-9_]+$"




            • ^ beginning of string,


            • [a-zA-Z_] 1 alpha character or underscore,


            • [\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,


            • $ end of string.




            So what you actually need is either:



            "^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"


            (Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)



            Or



            @"^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$"


            (No backslash substitution performed, so the regex engine directly interprets the escaped backslash)



            Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 2 '14 at 11:02









            JerryJerry

            58.2k1069102




            58.2k1069102













            • Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

              – Zanzi
              Oct 2 '14 at 12:08













            • Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

              – Zanzi
              Oct 2 '14 at 12:16











            • @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

              – Jerry
              Oct 2 '14 at 12:26













            • Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

              – Zanzi
              Oct 2 '14 at 12:34











            • @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

              – Jerry
              Oct 2 '14 at 13:06



















            • Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

              – Zanzi
              Oct 2 '14 at 12:08













            • Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

              – Zanzi
              Oct 2 '14 at 12:16











            • @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

              – Jerry
              Oct 2 '14 at 12:26













            • Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

              – Zanzi
              Oct 2 '14 at 12:34











            • @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

              – Jerry
              Oct 2 '14 at 13:06

















            Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

            – Zanzi
            Oct 2 '14 at 12:08







            Thanks for the answer, this seems to be the most complete one between the others. I needed to modify the regex to the following one, to match the fact the '' char is optional: ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]+$ I found that 1 character strings are not matched (e.g.: "a", "b", "9" etc. are not recognized). Why? Is it possible to fix that?

            – Zanzi
            Oct 2 '14 at 12:08















            Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

            – Zanzi
            Oct 2 '14 at 12:16





            Yes, it is!!! ^[a-zA-Z0-9_]+\?[a-zA-Z0-9_]*$ Thank you again

            – Zanzi
            Oct 2 '14 at 12:16













            @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

            – Jerry
            Oct 2 '14 at 12:26







            @Zanzi Sorry for not replying earlier. I got stuck with a big task to do :s If you want to avoid matching a you might want to use this instead: @"^[a-zA-Z0-9_]+(?:\[a-zA-Z0-9_]+$)?"

            – Jerry
            Oct 2 '14 at 12:26















            Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

            – Zanzi
            Oct 2 '14 at 12:34





            Maybe the following is better: ^[a-zA-Z0-9_]+(?:[a-zA-Z0-9_]+$)?$ Your solution also matches part of strings, instead I need to match the whole string.

            – Zanzi
            Oct 2 '14 at 12:34













            @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

            – Jerry
            Oct 2 '14 at 13:06





            @Zanzi Oops, I have put the closing paren at the wrong position. The $ shouldn't be inside, but completely at the end. Sorry about that.

            – Jerry
            Oct 2 '14 at 13:06













            1














            Don't make the as optional. The below regex won't allow two or more backslashes and asserts that there must be atleast one word character present before and after to the symbol.



            @"^w+\w+$"


            OR



            @"^[A-Za-z0-9_]+\[A-Za-z0-9_]+$"


            DEMO






            share|improve this answer





















            • 1





              errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

              – Chris
              Oct 2 '14 at 10:50













            • You added the rawing after reading my answer =/

              – Jerry
              Oct 2 '14 at 11:04











            • I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

              – Avinash Raj
              Oct 2 '14 at 11:05













            • see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

              – Avinash Raj
              Oct 2 '14 at 11:09













            • @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

              – Jerry
              Oct 2 '14 at 11:21
















            1














            Don't make the as optional. The below regex won't allow two or more backslashes and asserts that there must be atleast one word character present before and after to the symbol.



            @"^w+\w+$"


            OR



            @"^[A-Za-z0-9_]+\[A-Za-z0-9_]+$"


            DEMO






            share|improve this answer





















            • 1





              errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

              – Chris
              Oct 2 '14 at 10:50













            • You added the rawing after reading my answer =/

              – Jerry
              Oct 2 '14 at 11:04











            • I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

              – Avinash Raj
              Oct 2 '14 at 11:05













            • see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

              – Avinash Raj
              Oct 2 '14 at 11:09













            • @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

              – Jerry
              Oct 2 '14 at 11:21














            1












            1








            1







            Don't make the as optional. The below regex won't allow two or more backslashes and asserts that there must be atleast one word character present before and after to the symbol.



            @"^w+\w+$"


            OR



            @"^[A-Za-z0-9_]+\[A-Za-z0-9_]+$"


            DEMO






            share|improve this answer















            Don't make the as optional. The below regex won't allow two or more backslashes and asserts that there must be atleast one word character present before and after to the symbol.



            @"^w+\w+$"


            OR



            @"^[A-Za-z0-9_]+\[A-Za-z0-9_]+$"


            DEMO







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '14 at 11:04

























            answered Oct 2 '14 at 10:28









            Avinash RajAvinash Raj

            142k14115162




            142k14115162








            • 1





              errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

              – Chris
              Oct 2 '14 at 10:50













            • You added the rawing after reading my answer =/

              – Jerry
              Oct 2 '14 at 11:04











            • I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

              – Avinash Raj
              Oct 2 '14 at 11:05













            • see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

              – Avinash Raj
              Oct 2 '14 at 11:09













            • @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

              – Jerry
              Oct 2 '14 at 11:21














            • 1





              errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

              – Chris
              Oct 2 '14 at 10:50













            • You added the rawing after reading my answer =/

              – Jerry
              Oct 2 '14 at 11:04











            • I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

              – Avinash Raj
              Oct 2 '14 at 11:05













            • see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

              – Avinash Raj
              Oct 2 '14 at 11:09













            • @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

              – Jerry
              Oct 2 '14 at 11:21








            1




            1





            errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

            – Chris
            Oct 2 '14 at 10:50







            errr... w means a whole lot more than that. as I understand it. "A word character. A character in the input string can belong to any of the Unicode categories that are appropriate for characters in words. For more information, see Word Character." - msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx . Best example is yours will match accented characters and such like whereas the original will not. (of note is that the demo site you posted does not do c# regex matching).

            – Chris
            Oct 2 '14 at 10:50















            You added the rawing after reading my answer =/

            – Jerry
            Oct 2 '14 at 11:04





            You added the rawing after reading my answer =/

            – Jerry
            Oct 2 '14 at 11:04













            I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

            – Avinash Raj
            Oct 2 '14 at 11:05







            I thought c# regex was normally put inside a verbatim string. So i posted only the regex. For more clarification, i put them into @""

            – Avinash Raj
            Oct 2 '14 at 11:05















            see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

            – Avinash Raj
            Oct 2 '14 at 11:09







            see the revisions, op's original regex is ^[a-zA-Z_][]?[a-zA-Z0-9_]+$, chris edited it to ^[a-zA-Z_][\]?[a-zA-Z0-9_]+$. So it means op will pass the regex within a verbatim string.

            – Avinash Raj
            Oct 2 '14 at 11:09















            @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

            – Jerry
            Oct 2 '14 at 11:21





            @AvinashRaj Chris did not edit the regex. Only formatting (backticks and indent was added).

            – Jerry
            Oct 2 '14 at 11:21











            0














            The best way to fix up your regex is the following:



            ^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$



            This breaks down to:



            NODE                     EXPLANATION
            --------------------------------------------------------------------------------
            ^ the beginning of the string
            --------------------------------------------------------------------------------
            [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
            '0' to '9', '_' (1 or more times (matching
            the most amount possible))
            --------------------------------------------------------------------------------
            \ ''
            --------------------------------------------------------------------------------
            [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
            '0' to '9', '_' (1 or more times (matching
            the most amount possible))
            --------------------------------------------------------------------------------
            $ before an optional n, and the end of the
            string


            Explanation courtesy of http://rick.measham.id.au/paste/explain.pl



            As you can see we have the same pattern before and after the backslash (since you indicated they should both be letters, numbers and underscores) with the + modifier meaning at least one. Then in the middle there is just the backslash which is compulsory.



            Since it is unclear whether when you said "letters" you meant the basic alphabet or if you meant anything that is letter like (most obviously accented characters but also any other alphabet, etc.) then you may want to expand your set of characters by using something like w as Avinash Raj suggests. See http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#WordCharacter for more info on what the "word character" covers.






            share|improve this answer




























              0














              The best way to fix up your regex is the following:



              ^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$



              This breaks down to:



              NODE                     EXPLANATION
              --------------------------------------------------------------------------------
              ^ the beginning of the string
              --------------------------------------------------------------------------------
              [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
              '0' to '9', '_' (1 or more times (matching
              the most amount possible))
              --------------------------------------------------------------------------------
              \ ''
              --------------------------------------------------------------------------------
              [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
              '0' to '9', '_' (1 or more times (matching
              the most amount possible))
              --------------------------------------------------------------------------------
              $ before an optional n, and the end of the
              string


              Explanation courtesy of http://rick.measham.id.au/paste/explain.pl



              As you can see we have the same pattern before and after the backslash (since you indicated they should both be letters, numbers and underscores) with the + modifier meaning at least one. Then in the middle there is just the backslash which is compulsory.



              Since it is unclear whether when you said "letters" you meant the basic alphabet or if you meant anything that is letter like (most obviously accented characters but also any other alphabet, etc.) then you may want to expand your set of characters by using something like w as Avinash Raj suggests. See http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#WordCharacter for more info on what the "word character" covers.






              share|improve this answer


























                0












                0








                0







                The best way to fix up your regex is the following:



                ^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$



                This breaks down to:



                NODE                     EXPLANATION
                --------------------------------------------------------------------------------
                ^ the beginning of the string
                --------------------------------------------------------------------------------
                [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
                '0' to '9', '_' (1 or more times (matching
                the most amount possible))
                --------------------------------------------------------------------------------
                \ ''
                --------------------------------------------------------------------------------
                [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
                '0' to '9', '_' (1 or more times (matching
                the most amount possible))
                --------------------------------------------------------------------------------
                $ before an optional n, and the end of the
                string


                Explanation courtesy of http://rick.measham.id.au/paste/explain.pl



                As you can see we have the same pattern before and after the backslash (since you indicated they should both be letters, numbers and underscores) with the + modifier meaning at least one. Then in the middle there is just the backslash which is compulsory.



                Since it is unclear whether when you said "letters" you meant the basic alphabet or if you meant anything that is letter like (most obviously accented characters but also any other alphabet, etc.) then you may want to expand your set of characters by using something like w as Avinash Raj suggests. See http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#WordCharacter for more info on what the "word character" covers.






                share|improve this answer













                The best way to fix up your regex is the following:



                ^[a-zA-Z0-9_]+\[a-zA-Z0-9_]+$



                This breaks down to:



                NODE                     EXPLANATION
                --------------------------------------------------------------------------------
                ^ the beginning of the string
                --------------------------------------------------------------------------------
                [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
                '0' to '9', '_' (1 or more times (matching
                the most amount possible))
                --------------------------------------------------------------------------------
                \ ''
                --------------------------------------------------------------------------------
                [a-zA-Z0-9_]+ any character of: 'a' to 'z', 'A' to 'Z',
                '0' to '9', '_' (1 or more times (matching
                the most amount possible))
                --------------------------------------------------------------------------------
                $ before an optional n, and the end of the
                string


                Explanation courtesy of http://rick.measham.id.au/paste/explain.pl



                As you can see we have the same pattern before and after the backslash (since you indicated they should both be letters, numbers and underscores) with the + modifier meaning at least one. Then in the middle there is just the backslash which is compulsory.



                Since it is unclear whether when you said "letters" you meant the basic alphabet or if you meant anything that is letter like (most obviously accented characters but also any other alphabet, etc.) then you may want to expand your set of characters by using something like w as Avinash Raj suggests. See http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#WordCharacter for more info on what the "word character" covers.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 2 '14 at 11:01









                ChrisChris

                23.7k36082




                23.7k36082























                    0














                    Pretty sure this should work if i understood everything you wanted.



                    ^([a-zA-Z0-9_]+\[a-zA-Z0-9_]+)





                    share|improve this answer


























                    • You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                      – Chris
                      Oct 2 '14 at 11:02
















                    0














                    Pretty sure this should work if i understood everything you wanted.



                    ^([a-zA-Z0-9_]+\[a-zA-Z0-9_]+)





                    share|improve this answer


























                    • You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                      – Chris
                      Oct 2 '14 at 11:02














                    0












                    0








                    0







                    Pretty sure this should work if i understood everything you wanted.



                    ^([a-zA-Z0-9_]+\[a-zA-Z0-9_]+)





                    share|improve this answer















                    Pretty sure this should work if i understood everything you wanted.



                    ^([a-zA-Z0-9_]+\[a-zA-Z0-9_]+)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 2 '14 at 11:05

























                    answered Oct 2 '14 at 10:54









                    VajuraVajura

                    913614




                    913614













                    • You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                      – Chris
                      Oct 2 '14 at 11:02



















                    • You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                      – Chris
                      Oct 2 '14 at 11:02

















                    You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                    – Chris
                    Oct 2 '14 at 11:02





                    You'll want to anchor that to the beginning and end of the string to make sure you don't just match a substring of the tested string.

                    – Chris
                    Oct 2 '14 at 11:02


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26158883%2fregex-to-match-backslash-inside-a-string%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

                    Full-time equivalent

                    Bicuculline

                    さくらももこ