且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串末尾的匹配号

更新时间:2023-11-15 17:01:52

您可以将正则表达式与 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.