Regular Expression - Achievement unlocked
-
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
-
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 – ∞)
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:
-
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
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 -
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
-
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.
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
-
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
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.....
-
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.....
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
-
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
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.
-
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
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!
-
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
-
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
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
-
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
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"