0

I need to find all numbers in a string up until (but not including) anything that is not a number. If a number occours later on (after an occourance of a non-number-character) then it shouldn't be included either. I am trying with PHP's command preg_replace.

This question helped me to write the following pattern:

.+?(?=[^0-9])

Which though doesn't work in:

preg_replace("/.+?(?=[^0-9])/", "", $entry); 

where $entry is my string to search in.

As an example, I could have the following strings:

  • 001Aa.bc
  • 002a
  • 003a4/.
  • 004a(4)
  • 005 (4).string-d
  • 006-34

from which I want to have:

  • 001
  • 002
  • 003
  • 004
  • 005
  • 006

My pattern seems to work in test-sites, like http://gskinner.com/RegExr/. Is my use of the PHP command wrong?

Community
  • 1
  • 1
Steeven
  • 3,819
  • 8
  • 35
  • 67

1 Answers1

1

Use preg_match()

preg_match(/^(\d)+/, $search_me, $save_string_to_me);

While it's rubular and not explicitly php, I imagine something this simple works the same: http://rubular.com/r/aqZhNC4nJq

Sturm
  • 3,935
  • 3
  • 23
  • 38