Skip to main content
added 160 characters in body
Source Link

You can use a regular expression to recognize positive integers written in decimal expansion: they are the strings that consist only of decimal digits, where at least one of the digits is nonzero.

(string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string) 

This allows leading zeros (e.g. "0123"); if you don't want that, insist that a nonzero digit comes first.

(string-match "\\`[1-9][0-9]*\\'" my-string) 

If you use regular expressions for other things in the same piece of code, save the match data around your test.

(save-match-data (string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string)) 

Note that if you try to convert the string to an integer with string-to-int, Emacs may make a floating point approximation if the integer is too large. With parse-integer, you'll get an integer but with wraparound at 2N where N is the number of bits in an integer in your version of Emacs.

You can use a regular expression to recognize positive integers written in decimal expansion: they are the strings that consist only of decimal digits, where at least one of the digits is nonzero.

(string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string) 

This allows leading zeros (e.g. "0123"); if you don't want that, insist that a nonzero digit comes first.

(string-match "\\`[1-9][0-9]*\\'" my-string) 

If you use regular expressions for other things in the same piece of code, save the match data around your test.

(save-match-data (string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string)) 

Note that if you try to convert the string to an integer with string-to-int, Emacs may make a floating point approximation if the integer is too large.

You can use a regular expression to recognize positive integers written in decimal expansion: they are the strings that consist only of decimal digits, where at least one of the digits is nonzero.

(string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string) 

This allows leading zeros (e.g. "0123"); if you don't want that, insist that a nonzero digit comes first.

(string-match "\\`[1-9][0-9]*\\'" my-string) 

If you use regular expressions for other things in the same piece of code, save the match data around your test.

(save-match-data (string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string)) 

Note that if you try to convert the string to an integer with string-to-int, Emacs may make a floating point approximation if the integer is too large. With parse-integer, you'll get an integer but with wraparound at 2N where N is the number of bits in an integer in your version of Emacs.

Source Link

You can use a regular expression to recognize positive integers written in decimal expansion: they are the strings that consist only of decimal digits, where at least one of the digits is nonzero.

(string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string) 

This allows leading zeros (e.g. "0123"); if you don't want that, insist that a nonzero digit comes first.

(string-match "\\`[1-9][0-9]*\\'" my-string) 

If you use regular expressions for other things in the same piece of code, save the match data around your test.

(save-match-data (string-match "\\`[0-9]*[1-9][0-9]*\\'" my-string)) 

Note that if you try to convert the string to an integer with string-to-int, Emacs may make a floating point approximation if the integer is too large.