- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Well, we've been asked for advice, and bandwidth is practically free. I see no reason to presuppose that an enquiry about learning regexps is some bizzarro troll. So let's take it at face value.
Uncontroversial? Helpful? Me?
Buy thirty euros worth of coffee, sit down, and browse the web. Otherwise, buy a book (if that's how you learn), or buy the software (if that's how you learn).
I'm not going to dictate choices. It's just nice to know that they're out there.
Admin
Admin
Funny, my first troll post! I was OP of the C solution and hadn't imagined that it would start a flame war about sysadmin, C# and whatnot. I knew that the original program was in C# or Java or any other "high-level" "leaking abstraction" (see Joel Spolsky) language. As for the RegEx theoreticians, yes one can use RegEx for that, but one can also use a Howitzer to hunt ducks. I call them RegEx theoreticians because they think they are the fastest way of doing things while they completely forget (or ever knew) that they are heavy to start, especially in managed languages and they are not of O(n) complexity:
So I provided a rather optimized implementation of the problem at hand, in the language I work day by day. PS: for the one who responded to my post (MG), there is no supplemental copy in my routine, the inner loop is to find the next non blank character. The use of the temporary variable ch has the purpose to avoid a double dereferencing of the *p pointer that certain compiler (gcc 3.4.6) are unable to avoid, so I did it explicitly here.
Admin
captcha: vindico Is that a threat?
Admin
You can get Expresso for Windows/.NET for free:
http://www.ultrapico.com/Expresso.htm
It even has a builder tool and decodes regexes for you.
Admin
It could also be C or C++, syntactically.
I'll go away and whip myself, justifiably, with birch twigs. I think my inadvertent point still stands, though. If you're faced with a problem in language X, there is little or no point in providing a solution in language Y. And, as a small mea culpa, I can't imagine a C or C++ programmer writing anything like the code in the OP. Use pointers or use higher-level constructs. Whichever the language supports best.
Admin
Admin
The new programmer has screwed up big time. What about the LOC?
Admin
You shouldn't change it without asking the boss about it. You shouldn't ask the boss about it. Therefore.....
Admin
Another option for the OP is to sit down with an automata theory textbook. The seemingly canonical suggestion here is Sipser's, but I learned from a different one. Hopcroft's book probably goes more into depth on regular languages, etc. than Sipser's.
But what do I know; I'm just an ivory tower academic. This may not be the best way to learn REs in terms of how you'll use them in actual code. (And, as discussed earlier in this thread, it can fill you with misconceptions about how regex libraries actually behave in practice, because they tend to not actually use the techniques you'd learn this way.)
Admin
Sam's Teach Yourself Regular Expressions in 10 Minutes is a great introduction. After priming yourself with that, Google should have most the answers.
I really ought to get round to reading Mastering Regular Expressions but life is short :o)
Admin
Admin
Why not just use the trim function?
Admin
while ( $original != str_replace(" "," ",$original) ) ;
Admin
Oops.. :)
Admin
Ahh, that makes sense.
Admin
Heh. Snobol, back in the day....
label string " " = " " :s(label)
Not quite recursive, but not bad for a language that's been dead for, what, 30 years?
Admin
Mind you, the fact that nobody reads a three day old blog-posting stands in the way, somewhat.
I feel a Marshall McLuhan moment coming on.
Admin
Your plus-plusage may vary.
Admin
You win today's Out Of Left Field award. Well done.
Admin
Mostly agree, in principle, but if you implemented that, the next sound you would hear would be the WHOOMP as air rushed to fill the vacuum left by the remaining US programming jobs being sent overseas where they would have no such silly restrictions.
Admin
Oh. "assHOLE"! I though thought he meant "assHAT".
Thanks for clearing that up.
Admin
Thank you for not saying "How about finding a string in your date?"
Admin
Admin
what about something along the lines of:
String RemoveDupeSpaces(String itemDesc) { int to = 0; int from = 0; bool last_was_space = false; for(int scan = 0; scan < itemDesc.Length; ++scan) { bool is_this_space = itemDesc[from] == ' '; if(last_was_space && is_this_space) { ++from; } else { itemDesc[to++] = itemDesc[from++]; } last_was_space = is_this_space; } return itemDesc.Substring(0, to); }
If you're concerned about efficiency, you may as well do it yourself... right?
Admin
"As tempted as Don was to simply add in another bunch of stringReplace calls just for fun, he replaced the whole thing with this.
// pull double spaces regexReplace(itemDesc, "[ ][ ]+", " ");"
Oh. Well I guess that works too.
Admin
Step 1:
Convert the string variable to a linked list of byte characters
Step 2:
Walk the list and set bFoundSpace to true when a space is encountered, or to False when a space is not encountered.
If space is encountered while bFoundSpace is true, jump back to the prior element of the list and shift its pointer forward one character, then follow the pointer and continue as before.
Step 3:
Walk the linked list again to reassemble the string.
Step 4:
Fired.
Admin
I find this code much harder to understand than the regex code.
Admin
Weirdest spam evar.
Admin
Other sites are seeing this too; a quick search found this posting.
Admin
While s.count(" "): s.replace(" ", " ")
It's not rocket science.
Admin
Ah, I get it. The extra space in your first argument to the replace method was removed by the web formatting on this site.
BlueKitties actually posted this:
Admin
lol! yeah....... right..... never heard that around here before...
Admin
Haha, yeah it did clip my extra whitespace. Shame on you WTF admins, you could have just caused a WTF with that feature. =p
Admin
TRWTF is that the correction makes essentially the same mistake, it should (pedantically) be s/[ ]{2,}/ /g.
Admin
Oh really?
I just translated his code into vbs and in order for any appreciable impact to be evident the starting string had to be 72170 chars. It is just possible that some user may enter that many spaces into a text input. I suppose.
This will be my last visit for a while. It's an entertaining site at first. Then it get irritating or even saddening. The catty bickering is just such a waste of life. And I do use regular expressions.
Admin
I'm surprised the correct regex wasn't the original check-in, replaced by a code monkey who didn't understand it.