Jonathan Bird
Jonathan Bird

Reputation: 352

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 "[email protected] would become 12345.

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

!(\d+)@!

Upvotes: 0

Views: 173

Answers (4)

vks
vks

Reputation: 67968

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

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

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

Upvotes: 2

Poonam
Poonam

Reputation: 4631

Try this

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

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

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

Upvotes: 3

Forien
Forien

Reputation: 2763

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

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

Upvotes: 0

Related Questions