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. The Lounge
  3. Regular Expression - Achievement unlocked

Regular Expression - Achievement unlocked

Scheduled Pinned Locked Moved The Lounge
regexjsonquestion
50 Posts 26 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.
  • M Marco Bertschi

    I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

    ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

    Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

    Clean-up crew needed, grammar spill... - Nagy Vilmos

    F Offline
    F Offline
    Fabio Franco
    wrote on last edited by
    #37

    Marco Bertschi wrote:

    And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

    You didn't need to tell us what it will parse, it's immediatelly obvious what the regex is for. Anyone can see that.

    To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

    1 Reply Last reply
    0
    • M Matthys Terblanche

      Ah thanks. I'll grab it from home since DB is also blocked at work.:thumbsup:

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #38

      They are a friendly bunch, aren't they? :laugh: Do they allow you access to anything? :omg:

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • M Marco Bertschi

        I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

        ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

        Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

        Clean-up crew needed, grammar spill... - Nagy Vilmos

        T Offline
        T Offline
        Thornik
        wrote on last edited by
        #39

        Nothing beautiful, looks like a dump. [0-9] is just a \d.

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          They are a friendly bunch, aren't they? :laugh: Do they allow you access to anything? :omg:

          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

          M Offline
          M Offline
          Matthys Terblanche
          wrote on last edited by
          #40

          Yip, at least I can get onto CP, LOL. No social sites, so I hope they don't investigate the full capabilities of CP :^) ;) :doh:

          1 Reply Last reply
          0
          • B BillWoodruff

            JimmyRopes wrote:

            Codes Plz

            To hear is to obey, Master: [^] * * plain-vanilla text file

            “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

            J Offline
            J Offline
            JimmyRopes
            wrote on last edited by
            #41

            Ahhhhhhhhh. X| I knew I used RegEx for a reason. I have been using RegEx since my first stint at Bell Laboratories in the late 1970's. If you couldn't "grep" you had no street cred. Since then I have used RegEx on many platforms and in many languages. I don't even think about searching for patterns any other way. I know people who despise RegEx but it is to their detrement. They will have to code and debug a lot of code to do any kind of complex pattern recognition when they could use a RegEx and be done with it. As for the efficiency that will depend on whether the RegEx engine is context free NFA (Nondeterministic Finite Automaton) or context sensitive DFA (Deterministic Finite Automaton). In general context free is slower at complex pattern recognition but much easier to implement. Unless your application is time critical you will not notice any serious delay in processing as a result of using RegEx and they are easier to implement and debug. That is why I use them wherever I need to parse data. Just my opinion.

            The report of my death was an exaggeration - Mark Twain
            Simply Elegant Designs JimmyRopes Designs
            Think inside the box! ProActive Secure Systems
            I'm on-line therefore I am. JimmyRopes

            1 Reply Last reply
            0
            • M Marco Bertschi

              I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

              ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

              Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

              Clean-up crew needed, grammar spill... - Nagy Vilmos

              S Offline
              S Offline
              svella
              wrote on last edited by
              #42

              You could simplify/shorten it by replacing each instance of [0-9] with \d and getting rid of [] around single characters, though I suppose that could make it less readable.

              M 1 Reply Last reply
              0
              • S svella

                You could simplify/shorten it by replacing each instance of [0-9] with \d and getting rid of [] around single characters, though I suppose that could make it less readable.

                M Offline
                M Offline
                Marco Bertschi
                wrote on last edited by
                #43

                I already have improved it, with the help of OG: http://www.codeproject.com/Messages/4752996/Re-RegEx-problem-sharp2.aspx[^]

                Clean-up crew needed, grammar spill... - Nagy Vilmos

                1 Reply Last reply
                0
                • M Marco Bertschi

                  I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

                  ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

                  Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

                  Clean-up crew needed, grammar spill... - Nagy Vilmos

                  T Offline
                  T Offline
                  TheFigmo
                  wrote on last edited by
                  #44

                  I have managed to avoid this convoluted mess for over 30 years. And with a little luck, when i die i will STILL not speak regex. .net string methods work just fine for me. About as fast for 90% of your needs and more readable for 100% of your needs. The 2 or 3 times i have actually needed the power of regex in 30 years, i just sub'd out that line of code. And rather than learning/debugging/banging head .... I went to the beach swilling cheap whiskey and chasing cheaper women. My advice to those who dont yet know regex....impress your peers with cheap whiskey and women and forget about the regex. Both make your head hurt....but one is less fun.....

                  M 1 Reply Last reply
                  0
                  • T TheFigmo

                    I have managed to avoid this convoluted mess for over 30 years. And with a little luck, when i die i will STILL not speak regex. .net string methods work just fine for me. About as fast for 90% of your needs and more readable for 100% of your needs. The 2 or 3 times i have actually needed the power of regex in 30 years, i just sub'd out that line of code. And rather than learning/debugging/banging head .... I went to the beach swilling cheap whiskey and chasing cheaper women. My advice to those who dont yet know regex....impress your peers with cheap whiskey and women and forget about the regex. Both make your head hurt....but one is less fun.....

                    M Offline
                    M Offline
                    Marco Bertschi
                    wrote on last edited by
                    #45

                    I didn't found RegEx to be too hard, after finding out the basic principles at least. It's the same as with every new thing: Learning by doing!

                    Clean-up crew needed, grammar spill... - Nagy Vilmos

                    1 Reply Last reply
                    0
                    • M Marco Bertschi

                      I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

                      ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

                      Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

                      Clean-up crew needed, grammar spill... - Nagy Vilmos

                      S Offline
                      S Offline
                      syspau
                      wrote on last edited by
                      #46

                      I think this site is the best tutorial/guide to regular expressions: http://www.regular-expressions.info/tutorial.html Clear descriptions of everything from simple to complex. --- I like this site for testing expressions: http://www.regexr.com/ It's an online testing tool (plus a downloadable version if you like). Paste in your expression, paste in your text, see where it matches, with analysis, hover over matches to see groups.

                      1 Reply Last reply
                      0
                      • M Marco Bertschi

                        I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

                        ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

                        Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

                        Clean-up crew needed, grammar spill... - Nagy Vilmos

                        R Offline
                        R Offline
                        Ralph Little
                        wrote on last edited by
                        #47

                        You can probably compress it a little. If you are matching a single character, then you don't need []: [0] => 0 [:] => : [0][1][0-2][\.]? => 01[0-2]\.? .? I think needs to be quoted where you mean ".": . => \.? Also, you can often use meta characters to save some finger work: [0-9] => \d [0-9]{4} => \d{4} Also, some of the expressions don't need to be bracketed: ([0-9]{4}) => \d{4} You only need the brackets if you want the parser to be able to treat it as a unit in larger ops. Just some ideas. Nice work though!

                        1 Reply Last reply
                        0
                        • M Marco Bertschi

                          I had a bit help from an experienced colleague :laugh: The challenge is that it validates a Syslog-Timestamp. Problem? Yes. Here are some examples of valid timestamps: 2014-2-5T21:36:14.315Z-1.5 2014-2-5T21:36:14Z-1 2004-2-28T21:36:14.315Z+1.75 2004-2-29T21:36:14.315315Z+0 You see the problem X|

                          Clean-up crew needed, grammar spill... - Nagy Vilmos

                          K Offline
                          K Offline
                          KP Lee
                          wrote on last edited by
                          #48

                          Marco Bertschi wrote:

                          2004-2-29T21:36:14.315315Z+0

                          Just curious, how could Regex invalidate this timestamp: 2005-2-29T21:36:14.315315Z+0 or: 2100-2-29T21:36:14.315315Z+0

                          M 1 Reply Last reply
                          0
                          • K KP Lee

                            Marco Bertschi wrote:

                            2004-2-29T21:36:14.315315Z+0

                            Just curious, how could Regex invalidate this timestamp: 2005-2-29T21:36:14.315315Z+0 or: 2100-2-29T21:36:14.315315Z+0

                            M Offline
                            M Offline
                            Marco Bertschi
                            wrote on last edited by
                            #49

                            I have updated the RegEx meanwhile:

                            new Regex(@"(\d{4})-(0?[1-9]|1[0-2])-([0-2]?\d|3[0-1])T?([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)\.?(\d{0,6})?Z?([+-]?\d\.?\d?|\d{2}?|[0]?1[0-2]\.?\d?|\d{2}?)?", RegexOptions.IgnoreCase);

                            Clean-up crew needed, grammar spill... - Nagy Vilmos

                            1 Reply Last reply
                            0
                            • M Marco Bertschi

                              I had the joy to take my first steps with Regular Expressions today (I have tried to avoid them, but I eventually got that they are absolutely inavoidable, especially when it comes to parsing complex strings). So I present you my first RegEx:

                              ([0-9]{4})-([1-9]|[1][0-2])-([0-2]?[0-9]|[3][0-1])[T]([0-1]?[0-9]|[2][0-3])[:]([0-5]?[0-9])[:]([0-5]?[0-9])?.?([0-9]{1,6})[Z]([+-][0-9][\.|,]?[0-9]?|[0-9]{2}?|[+-]?[0][1][0-2][\.]?[0-9]?|[0-9]{2}?)

                              Beautiful, isn't she? :-O :-O :-O And that's the string it will parse: 2014-2-5T21:36:14.315Z+1.5

                              Clean-up crew needed, grammar spill... - Nagy Vilmos

                              D Offline
                              D Offline
                              dpminusa
                              wrote on last edited by
                              #50

                              One approach I have used with varying levels of utility is to treat the regex pattern like an algebraic expression. Factor out common terms and assign them to variables. Use replication counts, etc. This can simplify the final result and make it more maintainable. Your pattern can be simplified a bit and made more readable somewhat by this. I found other strings like valid IP addresses can be greatly simplified this way. Maybe this is useful to you!?

                              "Courtesy is the product of a mature, disciplined mind ... ridicule is lack of the same - DPM"

                              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