Steve
Hendra Kurniawan wrote:is there any way to simplify this? or is this the best way there is? thanks
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hendra Kurniawan wrote:Who knows if there's a one liner solution that's elegant out there. thanks
Hendra Kurniawan wrote:my brain is in the process of digesting regex, so I'm trying these out. I'm trying to find elegant solution to this seemingly simple problem, but ended up with the ugly syntax you guys saw up there. yes, breaking it into two matching processes definitely solves it, but I wish to know if there's a new technique that I can learn.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Junilu Lacar wrote:IMO, it's kind of hard to top the elegance of what Winston already gave;
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
fred rosenberger wrote:Don't forget the famous quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:You know? I had. Thanks for reminding me...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hendra Kurniawan wrote:So, everybody agrees that no "elegant" regex solution exists for this simple requirement?
Normally I did exactly like Winston did in his post, use loop to do the checking. simple and easy to understand.
as for speed, I believe the difference to be insignificant.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
as for speed, I believe the difference to be insignificant.
Then you need to read more. The expression I gave in my last post takes 25 seconds to parse 25 characters (or at least it did in 2008) and exhibits O(n^2) behaviour. And while I'm a firm advocate of Knuth's maxim (see also the quote below), you'll soon discover how good or bad a regex can be when you have a million lines of log files to check.
I'd also suspect that
".*([a-zA-Z].*\\d|\\d.*[a-zA-Z]).*"
is significantly faster than
"(.*[a-zA-Z]+.*\\d+.*|.*\\d.*[a-zA-Z]+.*)"
but I can't be bothered to test it. I also agree that neither is "elegant".
Speed is rarely a good reason for sacrificing readability and never for correctness; but in this case you've been offered solutions that do neither, and are almost certainly faster than your original expression.
"(.*[a-zA-Z]+.*\\d+.*|.*\\d.*[a-zA-Z]+.*)"
"(.*[a-z]+.*[A-Z]+.*\\d+.*|.*\\d+.*[a-z]+.*[A-Z]+.*|.*[A-Z]+.*[a-z]+.*\\d+.*|.*\\d+.*[A-Z]+.*[a-z]+.*|.*[a-z]+.*\\d+.*[A-Z]+.*|.*[A-Z]+.*\\d+.*[a-z]+.*)"
Hendra Kurniawan wrote:
So, everybody agrees that no "elegant" regex solution exists for this simple requirement? I'm also not a regex enthusiast. Hell, I just heard about regex several months ago. Let's just say I'm trying to explore new options to perform mundane tasks like validations and stuff. Normally I did exactly like Winston did in his post, use loop to do the checking. simple and easy to understand. as for speed, I believe the difference to be insignificant.
Henry Wong wrote:Anyway, here is my attempt... is it understandable?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hendra Kurniawan wrote:now, henry's solution seems very good as in very short...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |