Skip to main content
replaced http://emacs.stackexchange.com/ with https://emacs.stackexchange.com/
Source Link

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this postthis post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

last elisp code snippet incorrectly had the first code snippet instead of second as the first test case in the and function
Source Link

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (typepequal (stringnth 0 (re-toseq "[0-number9]+" "23K")) 'integer"23K") (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (typep (string-to-number "23K") 'integer) (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.

Source Link

Simple way to test if string contains positive integer

I'm trying to test for whether a string contains a positive integer (1, 2, 3, ...). By "string contains" I mean the string is only numbers (e.g. "1", "12", "123" but not "0", "1.1", "1a", "a1").

My first thought was to test the type and check if it is an integer but first I need to convert to numeric.

(typep (string-to-number "23K") 'integer) ;; t 

This doesn't work because string-to-number ignores any trailing characters.

Then I thought I'd use regular expressions to test that every character is in [0-9].

(equal (nth 0 (re-seq "[0-9]+" "23K")) "23K") 

where re-seq is taken from this post and searches for every occurrence of the regular expression (first arg) in the second arg.

Combining that with a test for the number being greater than 0 I end up with this:

(and (typep (string-to-number "23K") 'integer) (> (string-to-number "23K") 0) ) 

I'm pretty new to lisp programming so I'm wondering if there is a simpler way to do this.