-
-
Save chartjes/1774431 to your computer and use it in GitHub Desktop.
I need help extracting some info using a regex in PHP. | |
I have a string is like this | |
[email protected] | |
-> foo can change but is always lowercase alpha, variable length | |
-> tmp is ALWAYS tmp | |
-> uniqid is always lowercase alpha plus 0 to 9, output from uniqid() function | |
-> moontoast.com is ALWAYS moontoast.com | |
I need to extract the uniqid value from that string, using PHP. | |
Please help as my regex foo is weak | |
[email protected] |
How about ^[a-z]+-tmp-([a-z0-9]+)@moontoast.com$? That should capture the uniqid in backreference 1.
In case you missed it on twitter: /.-([\w\d]+)@./
<?php
$email = '[email protected]';
preg_match('/[a-z]+\-tmp\-([a-z0-9]+)@moontoast\.com/', $email, $matches);
$uid = $matches[1];
/((\w+)-tmp-([a-z0-9]+))@moontoast.com/
<?php
preg_match_all('/(\S+)@moontoast\.com/', $string, $match);
foreach($match as $entry) {
list($p0, $tmp, $p1) = explode("-", $entry[1]);
// ...
}
Will grab anything after the last - and before the @:
/-([^-]*)\@/
@stuartherbert @maxmanders @robertbasic @JCook21 All of yours have various issues: must escape the 'dot' in "moontoast.com" or it would match anything like "moontoastecom" (the dot represents any character), one doesn't take the 'tmp' into account, and a few accept anything in the uniqid portion.
EDIT: Same general things apply to all the ones here.
@dhorrigan Good point. Updated to escape the '.'.
gah! github ate my stars in the regex :(
A humble vote for the solution proposed by @shama. Seems like foo, tmp, and anything after the @ are irrelevant.
@robertbasic use backticks ;)
Perhaps I'm missing the point... apologies:
^.STAR-tmp-(.STAR)@moontoast.com
match in $1?
My asterisks aren't showing up so replace STAR with asterisk.