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

I need to match the last integer in a string and capture possible elements before and after the last in

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441555a4627854.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信