简体   繁体   中英

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:

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

Based on the answer of anubhava I formed a regular expression that works:

'/^(.+)\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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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