匹配字符串末尾的数字

时间:2022-09-19 17:58:11

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.

我不得不说弦的长度不是常数。

My language of choice is PHP.

我选择的语言是PHP。

Thanks.

谢谢。

3 个解决方案

#1


36  

You could use a regex with preg_match, like this :

您可以使用带有preg_match的regex,如下所示:

$string = "Conacu P PPL Europe/Bucharest 680979";

$matches = array();
if (preg_match('#(\d+)$#', $string, $matches)) {
    var_dump($matches[1]);
}

And you'll get :

和你会得到:

string '680979' (length=6)

And here is some information:

这里有一些信息:

  • 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 / )
  • regex开头和结尾的#是分隔符——它们没有任何含义:它们只是表示regex的开始和结束;你可以使用任何你想要的字符(人们经常使用/)
  • 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
    • 使用preg_match,作为第三个参数给出的数组将包含捕获的数据
    • 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 ()
    • 下一个将包含匹配为()的每个数据
  • ()表示您想要捕获它们之间的内容preg_match,作为第三个参数的数组将包含这些捕获的数据该数组中的第一项将是整个匹配的字符串下一项将包含一组()中匹配的每个数据
  • the \d means "a number"
  • \d的意思是“一个数字”
  • and the + means one or more time
  • +表示一次或多次

So :

所以:

  • match one or more number
  • 匹配一个或多个数字
  • at the end of the string
  • 在弦的末端

For more information, you can take a look at PCRE Patterns and Pattern Syntax.

要了解更多信息,可以查看PCRE模式和模式语法。

#2


2  

The following regex should do the trick:

下面的regex应该完成以下操作:

/(\d+)$/

#3


1  

EDIT: This answer checks if the very last character in a string is a digit or not. As the question https://*.com/q/12258656/1331430 was closed as an exact duplicate of this one, I'll post my answer for it here. For what this question's OP is requesting though, use the accepted answer.

编辑:这个答案检查字符串中的最后一个字符是否为数字。当问题https://*.com/q/12258656/1331430被关闭时,我将在这里给出我的答案。对于这个问题的OP要求,请使用已接受的答案。


Here's my non-regex solution for checking if the last character in a string is a digit:

下面是我的非regex解决方案,用于检查字符串中的最后一个字符是否是数字:

if (ctype_digit(substr($string, -1))) {
    //last character in string is a digit.
}

DEMO

演示

substr passing start=-1 will return the last character of the string, which then is checked against ctype_digit which will return true if the character is a digit, or false otherwise.

substr传递start=-1将返回字符串的最后一个字符,然后对ctype_digit进行检查,如果字符是一个数字,那么ctype_digit将返回true,否则返回false。

References:

引用:

  1. substr
  2. 字符串的子串
  3. ctype_digit
  4. ctype_digit

#1


36  

You could use a regex with preg_match, like this :

您可以使用带有preg_match的regex,如下所示:

$string = "Conacu P PPL Europe/Bucharest 680979";

$matches = array();
if (preg_match('#(\d+)$#', $string, $matches)) {
    var_dump($matches[1]);
}

And you'll get :

和你会得到:

string '680979' (length=6)

And here is some information:

这里有一些信息:

  • 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 / )
  • regex开头和结尾的#是分隔符——它们没有任何含义:它们只是表示regex的开始和结束;你可以使用任何你想要的字符(人们经常使用/)
  • 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
    • 使用preg_match,作为第三个参数给出的数组将包含捕获的数据
    • 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 ()
    • 下一个将包含匹配为()的每个数据
  • ()表示您想要捕获它们之间的内容preg_match,作为第三个参数的数组将包含这些捕获的数据该数组中的第一项将是整个匹配的字符串下一项将包含一组()中匹配的每个数据
  • the \d means "a number"
  • \d的意思是“一个数字”
  • and the + means one or more time
  • +表示一次或多次

So :

所以:

  • match one or more number
  • 匹配一个或多个数字
  • at the end of the string
  • 在弦的末端

For more information, you can take a look at PCRE Patterns and Pattern Syntax.

要了解更多信息,可以查看PCRE模式和模式语法。

#2


2  

The following regex should do the trick:

下面的regex应该完成以下操作:

/(\d+)$/

#3


1  

EDIT: This answer checks if the very last character in a string is a digit or not. As the question https://*.com/q/12258656/1331430 was closed as an exact duplicate of this one, I'll post my answer for it here. For what this question's OP is requesting though, use the accepted answer.

编辑:这个答案检查字符串中的最后一个字符是否为数字。当问题https://*.com/q/12258656/1331430被关闭时,我将在这里给出我的答案。对于这个问题的OP要求,请使用已接受的答案。


Here's my non-regex solution for checking if the last character in a string is a digit:

下面是我的非regex解决方案,用于检查字符串中的最后一个字符是否是数字:

if (ctype_digit(substr($string, -1))) {
    //last character in string is a digit.
}

DEMO

演示

substr passing start=-1 will return the last character of the string, which then is checked against ctype_digit which will return true if the character is a digit, or false otherwise.

substr传递start=-1将返回字符串的最后一个字符,然后对ctype_digit进行检查,如果字符是一个数字,那么ctype_digit将返回true,否则返回false。

References:

引用:

  1. substr
  2. 字符串的子串
  3. ctype_digit
  4. ctype_digit