2

i was struggling to make a regex which extract the digits bettween the last set of parentheses from a string, and this is what i came with until now:

^.*?\([^\d]*(\d+)[^\d]*\).*$

E.g.:

This is, (123456) a string (78910);

It returns 123456 , which is great but i need it to look at the last set and return 78910.Also , i want the regex to ignore everything but digits:

This is, (123bleah456) a string (789da10);

Should return: 78910

UPDATE

Using regex:

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

For string:

Telefon Mobil (123)Apple iPhone 6 128GB Gold(1567)asd234

Will return 234 when it should be 1567

Rubular

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Peter Cos
  • 398
  • 1
  • 2
  • 11
  • *Wiktor Stribiżew closed this question because he thinks it's a duplicate* - **It was a duplicate**. `(\d+)(?!.*\d)` worked well with the examples you provided. Now, you say the digits you need are inside the last `(...)`. Surely it is a different story. – Wiktor Stribiżew Nov 11 '16 at 11:05
  • @WiktorStribiżew , if you would have read it closer , you would have seen that i said that from the beginning,it is not a different story,even in the title it's mentioned that the digits are between parentheses.I removed my comment even though it was not offensive at all,it reflected your action.You marked as duplicate a question that was not everywhere near that – Peter Cos Nov 11 '16 at 11:23
  • Still, the answer is almost the same - [`\((\d+)\)(?!.*\(\d+\))`](https://regex101.com/r/8jgAfp/1) – Wiktor Stribiżew Nov 11 '16 at 11:24
  • @WiktorStribiżew also i don't like that you edited my question where i explained why i used a different regex on the update.That's power abuse. – Peter Cos Nov 11 '16 at 11:25
  • @WiktorStribiżew , on the other hand , \((\d+)\)(?!.*\(\d+\)) won't return the last parentheses's digits if it contains anything else other than digits.e.g (123mda321) – Peter Cos Nov 11 '16 at 11:29
  • https://regex101.com/r/8jgAfp/2 – Wiktor Stribiżew Nov 11 '16 at 11:34
  • @WiktorStribiżew , now it returns the last set of digits inside parenthese , but i need all digits inside to be extracted,ex: (123mda456) result: 123456 . – Peter Cos Nov 11 '16 at 11:39
  • Then you cannot do it with one regex *matching* operation. Extract the last parentheses with a digit inside, and remove all the digits as a second step. – Wiktor Stribiżew Nov 11 '16 at 11:42

1 Answers1

2

You can extract the last one by use of greed:

.*\(\K\d+

See demo at regex101

\K resets beginning of the reported match.

For your more specific updated case slightly modifiy the regex and strip out non-digits.

$str = "This is, (123bleah456) a string (789da10);";

if(preg_match('/.*\(\K\d[^)]*/', $str, $out))
  $res = preg_replace('/\D+/', "", $out[0]);

See demo at eval.in

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • That's great , but what if my paranthese contain something like this (123mda2456) . In this case i also need to extract only the numbers 123456 . How would you do that? – Peter Cos Nov 11 '16 at 11:36
  • boblbebubble: *[https://regex101.com/r/8jgAfp/2] returns the last set of digits inside parenthese , but i need all digits inside to be extracted,ex: `(123mda456)` result: `123456`.* – Wiktor Stribiżew Nov 11 '16 at 11:42
  • @PeterCos In this case use something like `.*\(\K\d[^)]*` and strip out non-digits with `preg_replace` and `\D+` on your result `$out[0]`. – bobble bubble Nov 11 '16 at 11:43