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. Regular expression

Regular expression

Scheduled Pinned Locked Moved C#
regexcssarchitecture
6 Posts 2 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.
  • H Offline
    H Offline
    hadad
    wrote on last edited by
    #1

    Hello, I am writing a regular expression to divide a CSS file into rules like this;

    #DTEST
    {
    border-style:solid;
    border-width:thin;
    }

    I want to repeat a part of the pattern to match all attributes enclosed between the tow braces the pattern I want to repeat looks like this: [\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]*

    Dad

    J 1 Reply Last reply
    0
    • H hadad

      Hello, I am writing a regular expression to divide a CSS file into rules like this;

      #DTEST
      {
      border-style:solid;
      border-width:thin;
      }

      I want to repeat a part of the pattern to match all attributes enclosed between the tow braces the pattern I want to repeat looks like this: [\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]*

      Dad

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      and your question?

      H 1 Reply Last reply
      0
      • J J4amieC

        and your question?

        H Offline
        H Offline
        hadad
        wrote on last edited by
        #3

        I want to repeat a part of the pattern to match all attributes enclosed between the two braces the pattern I want to repeat looks like this: [\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]* How to repeat this pattern? this pattern allows application to validate only one attribute like this color:red; but if there is a lot of attributes i don't know How to make the pattern match them all?

        Dad

        J 2 Replies Last reply
        0
        • H hadad

          I want to repeat a part of the pattern to match all attributes enclosed between the two braces the pattern I want to repeat looks like this: [\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]* How to repeat this pattern? this pattern allows application to validate only one attribute like this color:red; but if there is a lot of attributes i don't know How to make the pattern match them all?

          Dad

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Regex.Matches returns a MatchCollection, if more than one match is found. I don't see your problem!?!

          1 Reply Last reply
          0
          • H hadad

            I want to repeat a part of the pattern to match all attributes enclosed between the two braces the pattern I want to repeat looks like this: [\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]* How to repeat this pattern? this pattern allows application to validate only one attribute like this color:red; but if there is a lot of attributes i don't know How to make the pattern match them all?

            Dad

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Here you go, I even tested it for you...your pattern appears to work fine and dandy:

            string input = @"#DTEST
            {
            border-style:solid;
            border-width:thin;
            }";

            string pattern = "[\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]*";

            MatchCollection mc = Regex.Matches(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Console.WriteLine(mc.Count);
            foreach (Match m in mc)
            {
            Console.WriteLine(m.Value.Trim());
            }

            // output:
            // 2
            // border-style:solid
            // border-width:thin

            H 1 Reply Last reply
            0
            • J J4amieC

              Here you go, I even tested it for you...your pattern appears to work fine and dandy:

              string input = @"#DTEST
              {
              border-style:solid;
              border-width:thin;
              }";

              string pattern = "[\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]*";

              MatchCollection mc = Regex.Matches(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
              Console.WriteLine(mc.Count);
              foreach (Match m in mc)
              {
              Console.WriteLine(m.Value.Trim());
              }

              // output:
              // 2
              // border-style:solid
              // border-width:thin

              H Offline
              H Offline
              hadad
              wrote on last edited by
              #6

              Thanks a lot, I know that the pattern works well in the way you use it for getting the attributes inside these two braces {} but what I've failed to do is to match the whole rule like this:

              #DTEST
              {
              border-style
              : solid ;
              border-width : thin ;
              position:relative;
              right:10px;
              left:900px;
              width:50px;
              }

              This is the whole pattern: [\\.#]?[a-zA-Z0-9_]+[\n\t\\s]*{[\n\t\\s]*[a-zA-Z\\-]+[\\s]*:[\\s]*[a-zA-Z\\-]+[\n\t\\s]*;[\n\t\\s]*}

              but it works only if the css rule contains only one attribute like this:
              .Red
              {
              color:Blue;
              }

              the question is how can I let it match the css rule even if it has multiple attributes.

              Dad

              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