Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Issue with regular expression

Issue with regular expression

Scheduled Pinned Locked Moved C#
regexhelpsysadminjsonquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sebastian T Xavier
    wrote on last edited by
    #1

    Hello, I have an issue in regular expression matching...

    string x = "user = 'sa' password='eX65dere' server = 'localhost'";
    Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
    var r = rx.Match(x);
    if (r.Groups.Count > 0)
    {
    var g1 = r.Groups[0];
    MessageBox.Show(g1.ToString());
    }

    while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.

    Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");

    Can anyone help me on this?

    S L P 3 Replies Last reply
    0
    • S Sebastian T Xavier

      Hello, I have an issue in regular expression matching...

      string x = "user = 'sa' password='eX65dere' server = 'localhost'";
      Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
      var r = rx.Match(x);
      if (r.Groups.Count > 0)
      {
      var g1 = r.Groups[0];
      MessageBox.Show(g1.ToString());
      }

      while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.

      Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");

      Can anyone help me on this?

      S Offline
      S Offline
      Sebastian T Xavier
      wrote on last edited by
      #2

      I have solved this by changing the expression as follows...

      Regex rx = new Regex(@"password='([a-zA-Z0-9\\%^&_*]+)\'");

      Regards Sebastian

      1 Reply Last reply
      0
      • S Sebastian T Xavier

        Hello, I have an issue in regular expression matching...

        string x = "user = 'sa' password='eX65dere' server = 'localhost'";
        Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
        var r = rx.Match(x);
        if (r.Groups.Count > 0)
        {
        var g1 = r.Groups[0];
        MessageBox.Show(g1.ToString());
        }

        while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.

        Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");

        Can anyone help me on this?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Why are you escaping the underscore character? it is a normal character as far as Regex is concerned, just like + and - :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        P 1 Reply Last reply
        0
        • S Sebastian T Xavier

          Hello, I have an issue in regular expression matching...

          string x = "user = 'sa' password='eX65dere' server = 'localhost'";
          Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
          var r = rx.Match(x);
          if (r.Groups.Count > 0)
          {
          var g1 = r.Groups[0];
          MessageBox.Show(g1.ToString());
          }

          while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.

          Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");

          Can anyone help me on this?

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Simple. Underscore is not a metacharacter, so it doesn't need escaping (preceding with backslash). Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994.

          S 1 Reply Last reply
          0
          • L Luc Pattyn

            Why are you escaping the underscore character? it is a normal character as far as Regex is concerned, just like + and - :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            P Offline
            P Offline
            Peter_in_2780
            wrote on last edited by
            #5

            Snap!

            Software rusts. Simon Stephenson, ca 1994.

            1 Reply Last reply
            0
            • P Peter_in_2780

              Simple. Underscore is not a metacharacter, so it doesn't need escaping (preceding with backslash). Cheers, Peter

              Software rusts. Simon Stephenson, ca 1994.

              S Offline
              S Offline
              Sebastian T Xavier
              wrote on last edited by
              #6

              Got the idea, Thanks... again i have another question... Here is my updated expression...

              string x = "user = 'sa' password='e X65dere!@#$%^&*()' server = 'localhost'";
              Regex rx = new Regex(@"password=\'([a-zA-Z0-9\\!@#$%^&*|() _'""*-+{}<>,.;/?:~`\[\]\\\\]+)\'");
              var r = rx.Match(x);
              if (r.Groups.Count > 0)
              {
              var g1 = r.Groups[0];
              MessageBox.Show(g1.ToString());
              }

              In this case it will match for every small & large cap letters, numbers and special characters except = . If the password contains = the expression fails... can you please help?

              P 1 Reply Last reply
              0
              • S Sebastian T Xavier

                Got the idea, Thanks... again i have another question... Here is my updated expression...

                string x = "user = 'sa' password='e X65dere!@#$%^&*()' server = 'localhost'";
                Regex rx = new Regex(@"password=\'([a-zA-Z0-9\\!@#$%^&*|() _'""*-+{}<>,.;/?:~`\[\]\\\\]+)\'");
                var r = rx.Match(x);
                if (r.Groups.Count > 0)
                {
                var g1 = r.Groups[0];
                MessageBox.Show(g1.ToString());
                }

                In this case it will match for every small & large cap letters, numbers and special characters except = . If the password contains = the expression fails... can you please help?

                P Offline
                P Offline
                ProEnggSoft
                wrote on last edited by
                #7

                The reason is the quantifier + is greedy quantifier, that is it matches everything that matches the pattern up to the end of the input string. In your case it matches password='e X65dere!@#$%^&*()' server = 'localhost' when = is put in the pattern as every thing in between the first ' and last ' matches according to the pattern given when = included. To change this behaviour use the lazy quantifier that is +? instead of + alone.

                S 1 Reply Last reply
                0
                • P ProEnggSoft

                  The reason is the quantifier + is greedy quantifier, that is it matches everything that matches the pattern up to the end of the input string. In your case it matches password='e X65dere!@#$%^&*()' server = 'localhost' when = is put in the pattern as every thing in between the first ' and last ' matches according to the pattern given when = included. To change this behaviour use the lazy quantifier that is +? instead of + alone.

                  S Offline
                  S Offline
                  Sebastian T Xavier
                  wrote on last edited by
                  #8

                  great help....worked...thanks

                  P 1 Reply Last reply
                  0
                  • S Sebastian T Xavier

                    great help....worked...thanks

                    P Offline
                    P Offline
                    ProEnggSoft
                    wrote on last edited by
                    #9

                    Thank you

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups