Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numbers from string with regex

Tags:

string

regex

I am trying to write a regex to get the numbers from strings like these ones:

javascript:ShowPage('6009',null,null,null,null,null,null,null)
javascript:BlockLink('2146',null,null,null)

I am having difficulty writing the regex to grab these numbers.

How should I do this?

like image 228
RailsSon Avatar asked Jun 25 '09 12:06

RailsSon


People also ask

How do I capture a number in regex?

\d for single or multiple digit numbers To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

How do you find a number in a string?

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.

How do I extract numbers from a string in Python?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.


2 Answers

Try this:

(\d+)

What language are you using to parse these strings?

If you let me know I can help you with the code you would need to use this regular expression.

like image 103
Andrew Hare Avatar answered Oct 02 '22 00:10

Andrew Hare


Integer or float:

/\d+((.|,)\d+)?/
like image 22
bitreal665 Avatar answered Oct 01 '22 22:10

bitreal665