I'm up against a brick wall on this one ... coming into work with the top down seems to have addled my already troubled mind
My "in string" is of this format:
PHP Code:
$std_regex1="%[\w]{1,3}\d{1,5}%"; //A1, ABC12345
I want to separate the alpha character(s) from the numerics with a hyphen - the desired "out string" is "A-1" in the first example case, and "ABC-12345" in the second.
So far, I've drawn a blank on this with preg_replace() and was actually considering looping through the entire string to prepend a hyphen to the first instance of a numeric character, but I'm already way too expensive to scale this.
A good 1/2 hour of searching here and the big G isn't helping, either. Maybe more breakfast/coffee?
Any pointers are appreciated ... I'm sure I'm just being thick as usual.
Last edited by dalecosp; 10-24-2012 at 11:26 AM.
Reason: removed nasty whitespace from the regex and fix speling ;)
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
If it could get the hyphen one character sooner in the other cases it looks golden ... though I need to throw a few thousand test cases at it to be certain.
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
Blargh, yes, indeed. Just did it for myself. Thanks. I need a lunch break!
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Shouldn't have been using "\w" in the first place on this one, I think
It was imported from another function where it needed to be less restrictive.
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
I'd recommend something I find a lot simpler in this case. Create a pattern consisting of a looh behind assertion for a-zA-Z and a look ahead assertion for 0-9. That is, you'll be matching the spot where a letter is followed by a didigt, and replace that spot with a -. That there isn't an actual character in that particular spot doesn't matter, it works anyway.
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
Bookmarks