Live data from Hacker News

Stop avoiding regular expressions damn it

bradt.ca

21–30 of 55 posts

Re: Stop avoiding regular expressions damn it

#21
I found the house I currently live in with regular expressions.

A couple of years ago I moved to a different country, and for some reasons I needed two apartments, preferably close to each other. As you can imagine, the real estate websites are not designed for the kind of query I needed, so I wrote some code to aid me in my quest[1].

It's just shell script and text processing with awk. I download various results with all the available apartments for many real estate websites, then I scrape the data I care about (with regular expressions!) like address, rooms, price, anything really, and query the Google Maps API with all the addresses to retrieve the geographical coordinates, then I compute the distances between any two houses and sort them.

It's fantastically modular. Adding support for a new website meant just creating some regular expressions that work for that website. This was great because I was doing this on the road, as I was visiting the foreign city and found new sources of information.

Regular expressions were also great because these websites didn't have any API where I could query for the address, etc. I had to rely on what people wrote in their ads. This meant that when I wrote a regexp to match a set of results I had to inspect the failures to see new ways people described their houses and improved my matching based on that. Initially I had hoped I'd be able to parse 80% of the ads, but measurements and careful coding had allowed me to match approximately 99% of the ads!

The textual operation of this software allowed me to easily input some data manually. For example I realized that I'm also interested in having these apartments close to a subway station. No problem, just manually create the file with the subway stations in the correct, simple, textual format and the program will pick it up and use automatically.

The textual interface also helped with fancy queries, like "price between X and Y, 6 rooms total, prefer 4-2 to 3-3 if distance less than D, but 3-3 if distance greater than D, prefer Z subway line to Q, only one apartment might be from an agency rather than an individual, try to put one in K part of the city". Try to do that with an existing website.

[1] https://code.google.com/p/operation-housefinder/

Re: Stop avoiding regular expressions damn it

#22

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

Just curious: how do the regexes compare when you use "^@(.*)@$" ? Semantically, it's closer to the string version.

Realistically, you'd expect them to behave exactly the same, but Go's pretty new, and you never know what is or isn't going to be optimized.

Re: Stop avoiding regular expressions damn it

#23
post #16

Does anybody know of a good perl of python library that will use a regex (with constraints on the repetition operators) and generate an exhaustive list of matching strings (instead of generating a random list)? I think this would be helpful in many cases in getting people to understand how regexes work. I've seen lots of cases where toolsets designed to help people build regexes end up with them confused when their r…

https://github.com/ferno/greenery

Re: Stop avoiding regular expressions damn it

#24

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop.

MustCompile is generally used to make the regexp a global so that it isn't done over and over.

Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

Re: Stop avoiding regular expressions damn it

#25
post #22

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

Just curious: how do the regexes compare when you use "^@(.*)@$" ? Semantically, it's closer to the string version. Realistically, you'd expect them to behave exactly the same, but Go's pretty new, and you never know what is or isn't going to be optimized.

    `\A@(.*)@\z`

    BenchmarkRegexp	  500000	      5181 ns/op
    BenchmarkStrings	10000000	       171 ns/op

Re: Stop avoiding regular expressions damn it

#26
post #24

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop. MustCompile is generally used to make the regexp a global so that it isn't done over and over. Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

[deleted]

Re: Stop avoiding regular expressions damn it

#27
post #24

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop. MustCompile is generally used to make the regexp a global so that it isn't done over and over. Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

It does have the MustCompile outside of the loop. I pasted the wrong link originally.

Re: Stop avoiding regular expressions damn it

#28
post #4

THE single best ressource to really learn how to deal competently with regex is still Jeffrey Friedl's book "Mastering Regular Expressions". You will profit from it for the rest of your career. (There's also a Regex short reference and a Regex cookbook by O'Reilly...)

Sincere question - is it worth investing time into reading a 500 odd page book for something that I might not use that frequently in my career? From my experience, I've seen that I can get away by just Googling or just experimenting whenever I'm stuck on a regex.

You don't need to read 500 pages to understand the core of regex. "The core" means "what you will use 99% of the time". You need 11 minutes: http://www.youtube.com/watch?v=hwDhO1GLb_4

Re: Stop avoiding regular expressions damn it

#30
post #24

Earlier quoted context omitted.

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop. MustCompile is generally used to make the regexp a global so that it isn't done over and over. Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

It does have the MustCompile outside of the loop. I pasted the wrong link originally.

Ah, my apology I saw the earlier link.
Post reply on HN