Skip to main content

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) 

string-match-p can be used to check for numbers in a 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 question for a great overview. A few examples are given below.

With Emacs comes the variable eshell-number-regexp whose value is: "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?". This matches both integers and floats (including in scientific notation) and should suffice for many cases. To ensure an exact match (no leading or trailing characters) use it like this:

(string-match-p "^-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?$" STRING) 

Note however that this also matches things like 10e..

To only match positive integers:

(string-match-p "^[0-9]+$" STRING) 

To only match (positive or negative) integers:

(string-match-p "^[-]?[0-9]+$" STRING) 

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 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.

In the (most of the) regexes below it is assumed that explicit plus signs + are not allowed. To allow them, change the parts of the regexes matching a - below (i.e. - except when part of a number range) to [+-]. Moreover, it is assumed that the decimal separator is .; to allow , too simply add it directly after all occurances of .. See (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 (test):

"^-?\\(?:\\(?:\\(?:0\\|[1-9][0-9]*\\)?[.][0-9]+\\)\\|\\(?:0\\|[1-9][0-9]*\\)\\)$" (regex5) 

integers and floats without exponents, allowing for leading decimal point, and allowing leading zeros (test):

"^-?\\(?:[0-9]*[.]\\)?[0-9]+$" (regex6) 

integers and floats without exponents, allowing for leading and trailing decimal point, and allowing leading zeros (test):

"^-?\\(?:[0-9]+\\(?:[.][0-9]*\\)?\\|[.][0-9]+\\)$" (regex7) 

Match integers and floats with exponent (scientific notation)

integers 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 eshell-number-regexp surrounded with ^ and $ and using shy groups) (test):

"^-?\\(?:[0-9]*[.]\\)?[0-9]+\\(?:e[-0-9.]+\\)?$" (regex10) 

integers and floats with exponent, allowing for leading and trailing decimal point, leading zeros in base, and leading zeros in exponent (test):

"^[-]?\\(?:[0-9]+\\(?:[.][0-9]*\\)?\\(?:e[-]?[0-9]+\\)?\\|[.][0-9]+\\(?:e[-]?[0-9]+\\)?\\)$" (regex11) 

integers 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 (test):

"^[+-]?\\(?:[0-9]+\\(?:[.,][0-9]*\\)?\\(?:e[+-]?[0-9]+\\)?\\|[.,][0-9]+\\(?:e[+-]?[0-9]+\\)?\\)$" (regex12) 

How about, this for a positive integer?: (string-match-p "^[0-9]+$" STRING)

Or, this can be used to check for eithernumbers in a positive or negative integer? string using the syntax (string-match-p "^[-]?[0-9]+$"REGEXP STRING) where REGEXP is a string holding a regular expression.

Note that string-match-pdoes does not change match data, whereas string-match does. Matching numbers (integers and floats) using regexes is quite involved in general, see this Stackoverflow question for a great overview. A few examples are given below.

With Emacs comes the variable eshell-number-regexp whose value is: "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?". This matches both integers and floats (including in scientific notation) and should suffice for many cases. To ensure an exact match (no leading or trailing characters) use it like this:

(string-match-p "^-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?$" STRING) 

Note however that this also matches things like 10e..

To only match positive integers:

(string-match-p "^[0-9]+$" STRING) 

To only match (positive or negative) integers:

(string-match-p "^[-]?[0-9]+$" STRING) 

How about, this for a positive integer?: (string-match-p "^[0-9]+$" STRING)

Or, this for either a positive or negative integer? (string-match-p "^[-]?[0-9]+$" STRING)

Note that string-match-pdoes not change match data, whereas string-match does.

string-match-p can be used to check for numbers in a 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 question for a great overview. A few examples are given below.

With Emacs comes the variable eshell-number-regexp whose value is: "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?". This matches both integers and floats (including in scientific notation) and should suffice for many cases. To ensure an exact match (no leading or trailing characters) use it like this:

(string-match-p "^-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?$" STRING) 

Note however that this also matches things like 10e..

To only match positive integers:

(string-match-p "^[0-9]+$" STRING) 

To only match (positive or negative) integers:

(string-match-p "^[-]?[0-9]+$" STRING) 
added 90 characters in body
Source Link
lawlist
  • 19.7k
  • 5
  • 44
  • 128

How about, this for a positive integer?: (string-match-p "^[0-9]+$" STRING)

Or, this for either a positive or negative integer? (string-match-p "^[-]?[0-9]+$" STRING)

Note that string-match-pdoes not change match data, whereas string-match does.

How about, this for a positive integer?: (string-match "^[0-9]+$" STRING)

Or, this for either a positive or negative integer? (string-match "^[-]?[0-9]+$" STRING)

How about, this for a positive integer?: (string-match-p "^[0-9]+$" STRING)

Or, this for either a positive or negative integer? (string-match-p "^[-]?[0-9]+$" STRING)

Note that string-match-pdoes not change match data, whereas string-match does.

Post Undeleted by lawlist
Post Deleted by lawlist
Source Link
lawlist
  • 19.7k
  • 5
  • 44
  • 128
Loading