Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created February 8, 2012 22:10
Show Gist options
  • Save chartjes/1774431 to your computer and use it in GitHub Desktop.
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]
@maxmanders
Copy link

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.

@JCook21
Copy link

JCook21 commented Feb 8, 2012

How about ^[a-z]+-tmp-([a-z0-9]+)@moontoast.com$? That should capture the uniqid in backreference 1.

@robertbasic
Copy link

In case you missed it on twitter: /.-([\w\d]+)@./

@stuartherbert
Copy link

@dhrrgn
Copy link

dhrrgn commented Feb 8, 2012

<?php
$email = '[email protected]';
preg_match('/[a-z]+\-tmp\-([a-z0-9]+)@moontoast\.com/', $email, $matches);
$uid = $matches[1];

@jmcneese
Copy link

jmcneese commented Feb 8, 2012

/((\w+)-tmp-([a-z0-9]+))@moontoast.com/

@jakemcgraw
Copy link

<?php

preg_match_all('/(\S+)@moontoast\.com/', $string, $match);
foreach($match as $entry) {
  list($p0, $tmp, $p1) = explode("-", $entry[1]);
  // ...
}

@shama
Copy link

shama commented Feb 8, 2012

Will grab anything after the last - and before the @:
/-([^-]*)\@/

@dhrrgn
Copy link

dhrrgn commented Feb 8, 2012

@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.

@JCook21
Copy link

JCook21 commented Feb 8, 2012

@dhorrigan Good point. Updated to escape the '.'.

@robertbasic
Copy link

gah! github ate my stars in the regex :(

@tnorthcutt
Copy link

A humble vote for the solution proposed by @shama. Seems like foo, tmp, and anything after the @ are irrelevant.

@shama
Copy link

shama commented Feb 8, 2012

@robertbasic use backticks ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment