简体   繁体   English

在行尾匹配整数

[英]Match integers at end of line

I am looking for a way to check if a certain line is of the following form: 我正在寻找一种方法来检查某行是否具有以下格式:

[random text A-Za-z0-9 etc.] [integer] [integer]

So basically I want to check if the line ends with two integers and has some text before. 所以基本上我想检查行是否以两个整数结尾并且之前有一些文本。 I would also like to match those two integers and the random text part, since I need to process/store them separately. 我还想匹配这两个整数和随机文本部分,因为我需要分别处理/存储它们。

I would prefer suggestions based on regular expressions. 我希望基于正则表达式的建议。


You can use this regex: 您可以使用此正则表达式:

^(.*)(\d)(\d)$

PHP code: PHP代码:

if (preg_match('/^(.*)(\d)(\d)$/', $str, $m)) {
    print_r($m);
}

Based on the answer of anubhava I formed a regular expression that works: 基于anubhava的答案,我形成了一个有效的正则表达式:

'/^(.+)\s(\d+)\s(\d+)$/'

Test: 测试:

test line: Informatica 22 100
array(4) {
  [0]=>
  string(18) "Informatica 22 100"
  [1]=>
  string(11) "Informatica"
  [2]=>
  string(2) "22"
  [3]=>
  string(3) "100"
}

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

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