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. Matching Percent Symbol Using REGEX

Matching Percent Symbol Using REGEX

Scheduled Pinned Locked Moved C#
regexhelparchitecturequestion
8 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.
  • L Offline
    L Offline
    Liagapi
    wrote on last edited by
    #1

    Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.

    string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
    string escapedString = Regex.Escape("border-bottom");
    string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
    Regex rgx = new Regex(pattern);
    Match match = rgx.Match(matchedCss);
    string matchedString = match.Value;
    Console.WriteLine(matchedString);
    Console.ReadLine();

    The REGEX pattern above should have matched the substring

    border-bottom:1% solid black;

    but for some reason it did not. Please help me resolve this problem, thanks in advance.

    P K 2 Replies Last reply
    0
    • L Liagapi

      Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.

      string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
      string escapedString = Regex.Escape("border-bottom");
      string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
      Regex rgx = new Regex(pattern);
      Match match = rgx.Match(matchedCss);
      string matchedString = match.Value;
      Console.WriteLine(matchedString);
      Console.ReadLine();

      The REGEX pattern above should have matched the substring

      border-bottom:1% solid black;

      but for some reason it did not. Please help me resolve this problem, thanks in advance.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You don't need to escape the colon maybe? And probably not the semi-colon either? And after all the begging I had to do to get Chris to add a Regular Expressions Forum[^] , too. :sigh:

      L 1 Reply Last reply
      0
      • P PIEBALDconsult

        You don't need to escape the colon maybe? And probably not the semi-colon either? And after all the begging I had to do to get Chris to add a Regular Expressions Forum[^] , too. :sigh:

        L Offline
        L Offline
        Liagapi
        wrote on last edited by
        #3

        Hi, thanks for your reply. I have updated my pattern by eliminating the back slash in front of the colon and semicolon as you have suggested. However, it still does not work. Below is the updated pattern

        pattern = escapedString + @":\s?\d+\W(\s?\w+)*\s?;?\s?";

        P 1 Reply Last reply
        0
        • L Liagapi

          Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.

          string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
          string escapedString = Regex.Escape("border-bottom");
          string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
          Regex rgx = new Regex(pattern);
          Match match = rgx.Match(matchedCss);
          string matchedString = match.Value;
          Console.WriteLine(matchedString);
          Console.ReadLine();

          The REGEX pattern above should have matched the substring

          border-bottom:1% solid black;

          but for some reason it did not. Please help me resolve this problem, thanks in advance.

          K Online
          K Online
          Kenneth Haugland
          wrote on last edited by
          #4

          ehm:

                  string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
                  string escapedString = Regex.Escape("border-bottom");
                  string pattern = escapedString + @"\\:\\s?\\d+\\W(\\s?\\w+)\*\\;?\\s?";
                  Regex rgx = new Regex(pattern);
                  Match match = rgx.Match(inputString);
                  string matchedString = match.Value;
                  Console.WriteLine(matchedString);
          

          It works for me. At least when I set the inputString to match you expression.

          L 1 Reply Last reply
          0
          • L Liagapi

            Hi, thanks for your reply. I have updated my pattern by eliminating the back slash in front of the colon and semicolon as you have suggested. However, it still does not work. Below is the updated pattern

            pattern = escapedString + @":\s?\d+\W(\s?\w+)*\s?;?\s?";

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

            It works for string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}"; I put the code provided in VS and got: Error 1 The name 'matchedCss' does not exist in the current context F:\Project\PlayGround\Playground\Form1.cs 757 56 Playground So, what text are you actually trying to match?

            1 Reply Last reply
            0
            • K Kenneth Haugland

              ehm:

                      string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
                      string escapedString = Regex.Escape("border-bottom");
                      string pattern = escapedString + @"\\:\\s?\\d+\\W(\\s?\\w+)\*\\;?\\s?";
                      Regex rgx = new Regex(pattern);
                      Match match = rgx.Match(inputString);
                      string matchedString = match.Value;
                      Console.WriteLine(matchedString);
              

              It works for me. At least when I set the inputString to match you expression.

              L Offline
              L Offline
              Liagapi
              wrote on last edited by
              #6

              It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.

              P J 2 Replies Last reply
              0
              • L Liagapi

                It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.

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

                Rule 1: Whenever a problem goes away on its own, it will come back on its own.

                1 Reply Last reply
                0
                • L Liagapi

                  It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  Means the code that you were looking at was not the code that was running. Perhaps because it was running a prior build rather than with your updates.

                  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