Created
February 8, 2012 22:10
-
-
Save chartjes/1774431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
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 ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about ^[a-z]+-tmp-([a-z0-9]+)@moontoast.com$? That should capture the uniqid in backreference 1.