简体   繁体   中英

Regex take everything after word and before character in PHP

I'm trying to get regex to work to take everything after "test" and before "@" in an email so "test-12345@example.com would become 12345.

I've got this far to get it to return everything before the "@" symbol. (Working in PHP)

!(\d+)@!

Either you can use capturing groups and use the regex

test-(\d+)@

and use $1 or use lookaheads and behinds like (?<=test-)\\d+(?=@) which will just match 12345

(?<=test-)[^@]+

You can try this.No need to use groups.See demo.

https://regex101.com/r/eZ0yP4/28

You want everything between test and @ so don't use \\d .

$myRegexPattern = '#test([^@])*@#Ui';
preg_match ($myRegexPattern, $input, $matches);
$whatYouNeed = $matches[1];

Try this

$input = 'test-12345@example.com';
$regexPattern =  '/^test(.*?)\@/';
preg_match ($regexPattern, $input, $matches);
$whatYouNeed = $matches[1];
var_dump($whatYouNeed);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM