简体   繁体   English

正则表达式在PHP中使用单词之后和字符之前的所有内容

[英]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. 我正在尝试让正则表达式工作以在电子邮件中的“测试”之后和“ @”之前接受所有内容,因此“ test-12345@example.com”将变为12345。

I've got this far to get it to return everything before the "@" symbol. 我已经走了很远,才能返回“ @”符号之前的所有内容。 (Working in PHP) (使用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 并使用$1或像(?<=test-)\\d+(?=@)这样的前向和后向匹配12345

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

You can try this.No need to use groups.See demo. 您可以尝试此操作。无需使用群组。请参见演示。

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

You want everything between test and @ so don't use \\d . 您需要介于test@之间的所有内容,因此不要使用\\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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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