[thelist] condensing 4 regexes to 1?

der wert derwert at hotmail.com
Sun Aug 6 17:20:34 CDT 2006


first your pattern

$pattern =
"/(center: {lat: )(-?[0-9]{1,3}\.[0-9]{6})(,lng: 
)(-?[0-9]{1,3}\.[0-9]{6})/";

you don't need the selectors on the center && ,lng areas unless you will be 
using the matched text, if you will not be referencing these selections you 
should either remove the selectors or disabled the back reference for them 
which speeds up the regex engine

you disable the backreference like so

say you have a pattern with a selector (abc) to disable the backreference 
you would change it to look like this (?:abc)

so if you want to keep the selectors on those portions just to seperate the 
regex pattern to make it easier to read then the final pattern would look 
like this

$pattern =
"/(?:center: {lat: )(-?[0-9]{1,3}\.[0-9]{6})(?:,lng: 
)(-?[0-9]{1,3}\.[0-9]{6})/";


second, the backreference doesn't work in the way you think it does

the pattern you have here, I'll walk through what it does

$pattern =
"/(center: {lat: )(-?[0-9]{1,3}\.[0-9]{6})(,lng: )\2/";

it will first match "center: {lat: " and create a backreference to the text 
matched, the back reference would be \1

next "(-?[0-9]{1,3}\.[0-9]{6})" would match "57.138801" and create a 
backreference to the text matched as \2

then ",lng: " would be matched and a backreference would be created to the 
text matched as \3

now you have your backreference \2, first note that this isn't a valid 
reference to the backreference \2 since you have the pattern in double 
quotes, the double quotes makes this ASCII char 2 so since you have the 
double quotes this would need to instead be \\2

next the backreference still won't do what you want since it will now try to 
match "57.138801" because that's what the selector first matched, it 
references the text not the pattern itself

hope this helps

here's some further reading on the subject
http://www.regular-expressions.info/brackets.html

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/




More information about the thelist mailing list