So I've been working on my Compiled Website, and doing some more complex stuff like working with blocks of text to turn them into lists or <pre> blocks. Then I decided to be self-abusing and rewrite some code that dealt with HTMLizing plain text URLs. Hey, i can do that with a regular expression! Oh hell..
Regular expressions are a nightmare at best. The problem is ever-worsened the longer the expression needs to become.
There are three problems to solve:
- Testing quickly and easily
- Indenting and otherwise formatting
-
Commenting throughout the regular expression
Testing regular expressions ∞
Rubular is an online tool to help create regular expressions. It's simple and truly wonderful. Problem solved.
Hell, it even lets you make a permalink to your work. For complex stuff, I'd add that link right into my code comments.
Yes, it would be "best" to have some sort of system of easily testing offline, but frankly I don't want to deal with that sort of thing right now, and an online tool works well. I'd rather have some version of it offline though. I wonder if it's available for download. But then I'd probably have to deal with a server, or hack it to make it a regular script. Argh.
Formatting regular expressions ∞
x
Yeah, x. It's pretty simple, but like a lot of documentation, something essential like this isn't talked about "loudly" enough for my liking. Even tutorials out there seem to avoid this. I don't get it. This is definitely something to learn, especially for young programmers.
url = %r{ (^| ) (http://|ftp://|irc://|gopher://|file://) }x
Comments in regular expressions ∞
url = %r{ (^| ) (?# comment) (?# comment on its own line, and yes the closing bracket is required) (http://|ftp://|irc://|gopher://|file://) }x
You can also have comments mixed in with one-line regular expressions too. But.. seriously, why would you want to do that?
Last updated 2017-12-18 at 10:25:12