jit.str.regexp with leading zeros

Jeff Thompson's icon

Hi all, been banging my head against the wall for many, many hours.

I'm building a zip code lookup as part of a much larger project. It uses a txt file I got from the US Census Bureau.

When using numbers that do not start with a zero (ex: 55446), everything works great; when I use zip codes that start with zero (ex: 07042), however, problems ensue.

I can format a "re" message for jit.str.regexp for all numbers and everything seems like it should work just fine, but no.

The message to jit.str.regexp looks like this:
re 08904","\w*","\w*
which works fine when the number is larger:
re 68510","\w*","\w*

Why?

Patch needs the attached txt file, a reduced version of the full thing.

Thanks all, any help would be so awesome and appreciated!

Jeff

Max Patch
Copy patch and select New From Clipboard in Max.

Zachary Seldess's icon

Hey Jeff,

How about this? According to a quick google search, that second zip should be Highland Park, NE, and the patch only gives Highland, so you still have a bit more tweaking to do. If you can't get that working, post again.

How's NE treating you?

Zachary

Max Patch
Copy patch and select New From Clipboard in Max.

Jeff Thompson's icon

Surprisingly (or not), the first person answering my question is the only person on the forum I know personally... even though the statistical chances of that seem like they should be zero!

Thanks for taking a look, seems like what it really needed was a fresh set of eyes. Also thanks for getting ready of some extra objects.

Ok:

So it does work for one word city names, but not two. If I change the message box to say:
$1","\w*","\w* \w*
it works for two letter cities but now does not work for one letter ones.

Argh!

Also, I tried:
$1","\w*","\w*|\w* \w*
to search for either one or two but that didn't work at all.

I feel like I'm slowly getting sprintf and regexp but how I would love an hour or two with someone who really gets it.

I should also add that I found a really great Flash program (http://www.gskinner.com/RegExr) that lets you put in any text and try to match it. It was a huge help.

Luke Hall's icon

This should do the trick. I've changed the last bit of your regex so that instead of searching for \w word characters it searches for [^"]* anything except a "quote". This, along with the ZIP and state are stored in backreferences and formatted into a list which is [zl slice]-d at the end. This way it should return the city/town name no matter how many words it is. It also adds zero padding to the ZIP code if it is less than 5 numbers and throws an error if it is more than five.

lh

Max Patch
Copy patch and select New From Clipboard in Max.

Jeff Thompson's icon

Very nice and very clean! Thanks!

I'm not at all familiar with jstrigger: sort of like trigger and expr rolled into one? In this case, is it evaluating a string?

Also, why did you add the parentheses in the sprintf object? Does that change it's behavior or just to make it easier to see?

Luke Hall's icon

The [jstrigger] object is for formatting with javascript, it can be a bit like [expr] and [sprintf] depending on how you use it. Here it just finds the length of a string. The (parentheses) create back references which are output from a different outlet of the [jit.str.regexp] object. I use them to find the specific data you are interested inside the search string.

lh