Regular expressions might seem arcane, but if you do any kind of software, they are a powerful hacker tool. Obviously, if you are writing software or using tools like grep, awk, sed, Perl, or just about any programming language, regular expressions can simplify many tasks. Even if you don’t need them directly, regular expression searches can help you analyze source code, search through net lists, or even analyze data captured from sensors.
If you’ve been using regular expressions for a long time, they aren’t very hard. But learning them for the first time can be tedious. Unless you try your hand at regular expression crosswords. The clues are regular expressions and the rows and columns all have to match the corresponding regular expressions.
For example, consider this puzzle (which is already filled out):
This puzzle has a theme (The Beatles). The first across clue says that the two characters have to be an HE or an LL or one or more Os (two, then, in this case). The first down clue says the characters can’t be S, P, E, A, or K. So even though the second across clue says the lower left cell could be an A (in PLEASE), that would violate the down clue.
Of course, this is a super simple puzzle, but the site has many more puzzles of increasing difficulty. If you prefer your puzzles in hardware, that’s ok. If you want to work a different kind of puzzle, you might look at our Omnibus.
Addictive.
I’ve gone through Intermediate and already learned a few things!
Yep. Regex – lead balloons, they have a lot in common.
Here’s the REGEX rules –
1) Avoid Regex at all costs
2) Tell people you know all about regex when you don’t
3) Then you have to do that server side MOD_REWRITE, google it – someone else is sure to have done it before – then don’t tell anyone.
4) Now you’ve mastered regex
#1 might be why you don’t learn them. It is like saying “Driving is hard, so take the bus every chance you get.”
Just to beat someone else to it:
https://xkcd.com/208/
Ha!
#1 is because 1 *have* learnt regex! Try to use Apache MOD_REWRITE without regex!!!
It’s a pain to write efficient parsers (apart from MOD_REWRITE) and even then regex always comes with a significant performance cost.
If you need to do something with regex then someone has probably done it before and they have probably made it as efficient as it can be so google it! and save yourself a lot of time.
There are almost always alternatives to regex and they are almost always more efficient. In rare occasions you actually need regex.
And then there is that readability issue that makes it the worst code in the world!!!!
So tell me what very simple function this regex does …
/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD
Oh, and I almost forgot
If there is ever justification for killing another human being … it’s because they used regex in a (My)SQL query!!!
At first glance that looks like a regex for validating email addresses.
Regular expressions aren’t hard. What’s hard is the syntax your regular expression parser uses and to make your regexs actually do the thing you intend them to do…
http://www.drdobbs.com/embedded-systems/irregular-expressions/240157655 — someone made a library for this, but I can’t find a link off hand.
VerbalExpressions
github.com/VerbalExpressions
I was thinking of this one: https://github.com/benlast/grimace
Linked from the article, too, is the regular expression library of expressions: http://regexlib.com
That’s how the first level of this year’s gchq puzzle is solved.
http://i.huffpost.com/gen/3770798/thumbs/o-GCHQ-PUZZLE-570.jpg?2
To debug your regular expressions:
https://www.debuggex.com/
I think the real challenge and art to Regex is crafting them in such a way as to make your intentions clear to yourself and to others, especially after months or years have passed. If one is capable of doing this then I suspect they will instantly vanish in a flash of enlightenment and become an energy being of unlimited power.
There is nothing regular about regular expressions.
Heh, I’ve been using regular expressions to cheat at scrabble for over 20 years now.