字符串末尾的匹配号 [英] Match number at the end of the string
本文介绍了字符串末尾的匹配号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下字符串,我如何匹配其末尾的整个数字?
Given the following string how can I match the entire number at the end of it?
$string = "Conacu P PPL Europe/Bucharest 680979";
我必须告诉您字符串的长度不是恒定的.
I have to tell that the lenght of the string is not constant.
我选择的语言是PHP.
My language of choice is PHP.
谢谢.
推荐答案
您可以将正则表达式与 preg_match
,就像这样:
You could use a regex with preg_match
, like this :
$string = "Conacu P PPL Europe/Bucharest 680979";
$matches = array();
if (preg_match('#(\d+)$#', $string, $matches)) {
var_dump($matches[1]);
}
然后您会得到:
string '680979' (length=6)
以下是一些信息:
- 正则表达式开头和结尾的
#
是定界符-它们没有任何意义:它们仅表示正则表达式的开头和结尾;并且您可以使用任何想要的字符(人们经常使用/
) - 模式结尾处的'$'表示字符串结尾"
-
()
表示您想捕获它们之间的内容- 使用
preg_match
,作为第三个参数给出的数组将包含那些捕获的数据 - 该数组中的第一项将是整个匹配的字符串
- ,接下来的将包含在一组
()
中匹配的每个数据
- The
#
at the beginning and the end of the regex are the delimiters -- they don't mean anything : they just indicate the beginning and end of the regex ; and you could use whatever character you want (people often use/
) - The '$' at the end of the pattern means "end of the string"
- the
()
means you want to capture what is between them- with
preg_match
, the array given as third parameter will contain those captured data - the first item in that array will be the whole matched string
- and the next ones will contain each data matched in a set of
()
所以:
- 匹配一个或多个数字
- 在字符串末尾
有关更多信息,请查看 PCRE模式和
Pattern Syntax
.For more information, you can take a look at PCRE Patterns and
Pattern Syntax
.这篇关于字符串末尾的匹配号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- with
- 使用
查看全文