正则表达式,获取两个字符之间的字符串值 [英] Regex, get string value between two characters
本文介绍了正则表达式,获取两个字符之间的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想返回两个字符@和点(.)之间的字符串.
I'd like to return string between two characters, @ and dot (.).
我尝试使用正则表达式,但是找不到它.
I tried to use regex but cannot find it working.
(@(.*?).)
有人吗?
推荐答案
您的正则表达式几乎可以正常工作,只是忘了转义句点.另外,在PHP中,您需要定界符:
Your regular expression almost works, you just forgot to escape the period. Also, in PHP you need delimiters:
'/@(.*?)\./s'
s是 DOTALL修饰符.
这是如何在PHP中使用它的完整示例:
Here's a complete example of how you could use it in PHP:
$s = 'foo@bar.baz';
$matches = array();
$t = preg_match('/@(.*?)\./s', $s, $matches);
print_r($matches[1]);
输出:
bar
这篇关于正则表达式,获取两个字符之间的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文