最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Regular expression to match the last integer in a string with php and javascript - Stack Overflow

programmeradmin2浏览0评论

I need to match the last integer in a string and capture possible elements before and after the last integer, assuming that the string can be posed of a single integer or a bination of text and/or integers. Let say that $str can be:

  • '123' -> '' - 123 - ''
  • 'abc 123' -> 'abc ' - 123
  • 'abc 123 def' -> 'abc ' - 123 - ' def'
  • 'abc 123 def 456' -> 'abc 123 def ' - 456
  • etc.

I expected that the following piece of code in php would do the job:

$str = 'abc 123 def 456 ghi';
preg_match('/(.*)(\d+)(\D*)$/', $str, $matches);
echo 'Matches = ' . implode(' : ', $matches);

But the (\d+) is picking up only one digit:

 Matches = abc 123 def 456 ghi : abc 123 def 45 : 6 :  ghi

While I was expecting:

 Matches = abc 123 def 456 ghi : abc 123 def  : 456 :  ghi

I get the same behavior in Javascript. The (.*) is that greedy to impinge on (\d+)?

Thanks in advance!

I need to match the last integer in a string and capture possible elements before and after the last integer, assuming that the string can be posed of a single integer or a bination of text and/or integers. Let say that $str can be:

  • '123' -> '' - 123 - ''
  • 'abc 123' -> 'abc ' - 123
  • 'abc 123 def' -> 'abc ' - 123 - ' def'
  • 'abc 123 def 456' -> 'abc 123 def ' - 456
  • etc.

I expected that the following piece of code in php would do the job:

$str = 'abc 123 def 456 ghi';
preg_match('/(.*)(\d+)(\D*)$/', $str, $matches);
echo 'Matches = ' . implode(' : ', $matches);

But the (\d+) is picking up only one digit:

 Matches = abc 123 def 456 ghi : abc 123 def 45 : 6 :  ghi

While I was expecting:

 Matches = abc 123 def 456 ghi : abc 123 def  : 456 :  ghi

I get the same behavior in Javascript. The (.*) is that greedy to impinge on (\d+)?

Thanks in advance!

Share Improve this question edited May 11, 2012 at 22:16 Raphael asked May 11, 2012 at 21:44 RaphaelRaphael 33 bronze badges 1
  • 1 Wouldn't using just /(\d+)/ do the trick? – Kep Commented May 11, 2012 at 21:47
Add a ment  | 

5 Answers 5

Reset to default 6

Why not /(\d+)\D*$/? That way, the only capturing group is the integer.

Btw, when I'm working with regexes, i usually use http://gskinner./RegExr/

In Javascript:

$str = 'abc 123 def 456';
$matches = $str.match(/\d+/g);
$lastInt = $matches[$matches.length-1];

In PHP

$str = 'abc 123 def 456';
preg_match_all('/(\d+)/', $str, $matches);
$lastInt = end($matches);
echo 'Last integer = ' . $lastInt;

You can pletely avoid the .* as the "\D*$" at the end of the pattern would make sure it is the last number.

It is also remended that you don't add unnecessary parenthesis and only get what you really need. That is to say that you could do the following to only get the last number:

preg_match('/(\d+)\D*$/', $str, $matches);

If you do need the match on the other portions of the string, though, and the number you are looking for will be its own word you could add to that regexp the \b parameter before the (\d+) so that the (.*) won't greedily consume part of your number. i.e.:

preg_match('/(.*)\b(\d+)(\D*)$/', $str, $matches);

This should do it for ya...

(\d+)(?!.*\d) 

If you want to cach the last integer, you should use the $ at the end of the regular expression.

Try with /(\d+)$/ in both languages.

发布评论

评论列表(0)

  1. 暂无评论