They're good for validating complex input, such as telephone numbers. They're also good for quickly grabbing stuff from strings of input, or for manipulating strings.
To give 2 real world examples -
Many years ago I used to play a game called Planetarion - a web based online space strategy game. In it you could scan other planets to determine if they were worth attacking. Groups of players would form alliances and you'd want to share these scans. Unfortunately the only way to get them was to copy and paste from the browser. To deal with this many alliances would write little web based tools that their members could paste the scan into to update the database. Although each browser would copy a table into plain next in roughly the same format, it wasn't exact - some would place tabs between columns, others spaces. Regular expressions were the most sane and sensible way of getting the required data from these pasted scans and into a relational database for analysis.
I use regex's most days at work. At work I'm employed to be an outsourced sysadmin, but I also code for internal projects and so forth. For both elements of my job I will use regexs. For instance, using ldifde you can query an Active Directory server to get various bits of information about stuff in the AD (such as getting a list of users in an OU [organisational unit]). Unfortunately it normally returns a load of extra stuff you don't want and can't trim out through ldifde itself. Using regex's you can reliably remove these additional bits of guff. In our internal projects regex's are use for some of the more complex form validation, such as post/zip codes, email addresses and phone numbers.
For the coding aspect of my job it's handy when moving blocks of code or for refactoring. For instance I recently had to convert from using the Zend_Date object to our own. There were about 3 affected files, and about 4 instances created in each. I could've manually gone through and edited each file and each instance. However, using regex's I could simply perform :%s/Zend_Date/Better_Date/g (This is an example of using Vim's regex support to subsitute Better_Date for Zend_Date, through out the entire file, even if it occurs multiple times in the same line).
Unfortunately it becomes exceptionally easy to over use regexs.
For instance although you could use a regex to screen scrape from a website, it's a bad idea and you should really be using something that understands the DOM and allows you to query based on that - such as XPath or CSS selectors - the reason being that although a regex will probably work, it'll break if the website introduces new lines or additional attributes that your regex doesn't have support for. Something that understands the DOM will just continue working.
As I've said before, regex's are something you will absolutely hate. Until you get used to them.
I must be strange.. I actually like RegEx! I find them an interesting challenge (for want of a better phrase). Granted, they can cause hair loss, but still provide more fun to me than trying to do a Rubik Cube for example
It bugs me when coding in C++ as it's not a language I'll claim to be fluent in at all that I haven't yet grasped the use of RegEx construction and normally have to resort to using various split() type calls and arrays to finally drill down to the data I need to access.
One task I've been asked to resolve at work is to fix some things in a CSV file. The 2 fields clearly labelled 'first_name' and 'last_name' seem to occasionally get filled with 'Miss Blogs' and 'Miss Joe' or the 'first_name' field with 'Jack or Jill' and the 'last_name' field left blank (don't ask.. we're talking about the acting industry.. not renowned for their intelligence). If I had to do this with C++, due to my lack of knowledge, I'd have to split each line at the ',' char, then process the fields working on splitting at spaces and the likes. Using RegEx will make the task very simple as I only need to split the row at the ',' char, then work on the relative field with some simple RegEx. I expect nothing more than 20 lines of code to read in a file, process and write an updated version.
Maybe sadistic, but I find them fun to work with (although my usage is pretty much limited to Perl and PHP)
Heh.. have been sorting out trying buy a house and spend more time round my fiancé's place than I do in front of the screen after work these days... not a bad thing though
I've never used Lex, so I can't really comment on that in any detail, but it's basically a library that allows you to write the language grammar directly in EBNF form as plain C++ code. Lex requires preprocessing if I'm not mistaken? From what Wikipedia tells me Spirit fills the role of Yacc as well (parser generator), so Spirit = Lex + Yacc I guess you could say.
I've been playing around with VS2010 and it's really good actually. I was concerned, like many people were, about it being ported to WPF, but it runs very smoothly and the font rendering in the editor is crisp and sharp. If anything I'd say it runs a bit faster than VS2008.
In terms of the new features in .NET 4.0, I've not had time to play around with much of it yet, but I'm enjoying some of the new additions to C# 4.0 (SortedSet is awesome), and the WPF editor has received a lot of work and is much more robust.
I think the best thing though has to be the improvements to the WPF font rendering, the text looks perfect now and makes me actually want to write WPF apps.
Are you kidding, or are the .NET bits really that different from the C++ ones?
In my experience 2010 beta 2 is slow as molasses and horribly buggy to boot. Things like "go to declaration/definition" are completely unusable since they usually lock up the entire IDE for many seconds while it fails to find the declaration I asked for. Intellisense is still completely broken, even though they bragged that they had fixed it this time. It also uses more than twice the memory doing the same thing as VS2008, which on my 2GB laptop means even a simple compile of my framework leads to massive disk swapping.
Quite frankly I find it an unusable mess. If it weren't for the C++ compiler improvements (which are also rather buggy) I'd stay with VS2008.
Yeah, that was another thing that put me off VS, I could type faster then it could render on the screen. I needed something lightweight and fast, so I use a NotePad2 Modification / Extension / Enhancement by Kai Liu for my programming needs.
Well I can only talk from my own experience, the C# editor is not any slower than in VS2008 for me, and the WPF editor is much, much better. That being said, the WPF support was awful before. I can understand that other potions of VS may not be all hugs and puppies, but for what I mainly use it for I'm pretty satisfied. Of course it is still a beta, and it does have a few bugs, but then that's what betas are for.
Ever read some of your old source code and become, like REALLY embarrassed by it? You think to yourself, how where you ever that stupid? I bring this up, because I'm looking at SimFIA's code base, as I was just going to release it, then I realized that it was really, really bad code, and I would hate it if anyone picked up some bad programming habits from it.
Echoing huge blocks of raw HTML ... Oh, it was bad!
Yes, every time. I think that it's a good thing though, as to me it means I'm still improving. If I ever look back and think my old code is perfect, then it may well be time to give up. That doesn't mean I'm not embarrassed by it! Most of it.
One thing though that it took me a long time to learn, is that when you release code no one really notices how badly written it is. No one really cares. I guess cause everyone secretly thinks all their code is badly written too and doesn't want to draw attention to themselves.
In my view, no matter how something was written, if it works well and is useful to people, then it's something you should be proud of and you shouldn't hide away. Plus, I guarantee you that pretty much every other programmer in the world has definitely seen, and probably written, worse.
Layouts ---------------------------------------- | id | int, auto_increment | | user_id | int | | etc... ----------------------------------------
Explanation - all users are stored in "users" table. Each user can have some layouts uploaded, so each layout is stored in "layouts" table also with the user ID of the uploader. All I want to do is, to do a SELECT query that would look like this and should be orderable by all columns.
Select all users and number of rows in layouts table with their ID ---------------------------------------- | username | number_of_layouts | ---------------------------------------- | Flame | 3 | | Jack | 5 | | Peter | 0 | ----------------------------------------
de Souza: I edited your query a little bit (yours didn't work as expected, returned one user and total number of all layouts). Now it returns all users and no. of layouts, but only from the users, who have at least 1 layout uploaded. I want this query to show a list of all users, so those, who have 0 layouts too. Here is my query so far:
SELECT U.username, COUNT(L.user_id) AS number_of_layouts FROM users U, layouts L WHERE U.id = L.user_id GROUP BY U.id
Edit: As I am thinking about it, it isn't necessary to show all registered users on my site, so I will use the query above for now. You can still post any thoughts