string-match-p can be used to check for numbers in a string STRING using the syntax (string-match-p REGEXP STRING) where REGEXP is a string holding a regular expression.
Note that string-match-p does not change match data, whereas string-match does.
Matching numbers (integers and floats) using regexes is quite involved in general, see this Stackoverflow questionthis Stackoverflow question (or this one) for a great overview. A few examples are given below. To test them in the browser click the test link. To test them in Emacs use M-x re-builder.
With Emacs comesIn the variable(most of the) regexes below it is assumed that explicit plus signs eshell-number+ are not allowed. To allow them, change the parts of the regexes matching a -regexp whose value is: below (i.e. "-?\\([0 except when part of a number range) to [+-9]*\\]. Moreover, it is assumed that the decimal separator is .\\)?[0-9]+\\(e[-0-9; to allow , too simply add it directly after all occurances of .]+\\)?". This matches bothSee (regex12) for an example. It is also assumed that signed zeros are allowed.
Match only integers
positive integers (test):
"^\\(?:0\\|[1-9][0-9]*\\)$" (regex1) positive integers, allow leading zeros (test):
"^[0-9]+$" (regex2) (positive or negative) integers (test):
"^-?\\(?:0\\|[1-9][0-9]*\\)$" (regex3) (positive or negative) integers, allow leading zeros (test):
"^-?[0-9]+$" (regex4) Match intergers and floats without exponent
integers and floats without exponents, allowing for leading decimal point (including in scientific notationtest):
"^-?\\(?:\\(?:\\(?:0\\|[1-9][0-9]*\\)?[.][0-9]+\\)\\|\\(?:0\\|[1-9][0-9]*\\)\\)$" (regex5) integers and should sufficefloats without exponents, allowing for many cases. To ensure an exact matchleading decimal point, and allowing leading zeros (notest):
"^-?\\(?:[0-9]*[.]\\)?[0-9]+$" (regex6) integers and floats without exponents, allowing for leading orand trailing charactersdecimal point, and allowing leading zeros (test) use it like this:
(string-match-p "^-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?$" STRING) "^-?\\(?:[0-9]+\\(?:[.][0-9]*\\)?\\|[.][0-9]+\\)$" (regex7) Match integers and floats with exponent (scientific notation)
Note however that this also matches things likeintegers and floats with exponent, allowing for leading decimal point (test):
"^-?\\(?:\\(?:\\(?:0\\|[1-9][0-9]*\\)?[.][0-9]+\\)\\|\\(?:0\\|[1-9][0-9]*\\)\\)\\(?:e-?\\(?:0\\|[1-9][0-9]*\\)?\\)?$" (regex8) integers and floats with exponent, allowing for leading decimal point, leading zeros in base, and leading zeros in exponent (test):
"^-?\\(?:[0-9]*[.]\\)?[0-9]+\\(?:e-?[0-9]+\\)?$" (regex9) integers and floats with exponent, allowing for leading decimal point, leading zeros in base, and overly permissive exponent (this is 10e.eshell-number-regexp. surrounded with ^ and $ and using shy groups) (test):
"^-?\\(?:[0-9]*[.]\\)?[0-9]+\\(?:e[-0-9.]+\\)?$" (regex10) To only match positive integers and floats with exponent, allowing for leading and trailing decimal point, leading zeros in base, and leading zeros in exponent (test):
(string-match-p "^[0-9]+$" STRING) "^[-]?\\(?:[0-9]+\\(?:[.][0-9]*\\)?\\(?:e[-]?[0-9]+\\)?\\|[.][0-9]+\\(?:e[-]?[0-9]+\\)?\\)$" (regex11) To only matchintegers and floats with exponent, allowing for leading and trailing decimal point, leading zeros in base, leading zeros in exponent, + signs, and , as alternative decimal separator (positive or negativetest) integers:
(string-match-p "^[-]?[0-9]+$" STRING) "^[+-]?\\(?:[0-9]+\\(?:[.,][0-9]*\\)?\\(?:e[+-]?[0-9]+\\)?\\|[.,][0-9]+\\(?:e[+-]?[0-9]+\\)?\\)$" (regex12)