A possible implementation: find the first digit, get the substring of the original string that starts at that first digit and then call string-to-number
on the result:
(defun first-number-of-string (s)
(string-to-number (substring s (string-match-p "[0-9]" s))))
(first-number-of-string "bm16" -> 16
(first-number-of-string "abc123def456" -> 123
(first-number-of-string "abc123") -> 123
(first-number-of-string "123def") -> 123
(first-number-of-string "123") -> 123
(first-number-of-string "abcdef") -> 0
That's assuming that the answers to the questions in my comment are that you indeed want a number, not its string representation, and in the case where there is no number in the string, having the function return 0
is acceptable.
first-number-from-string
in my emacs: how is it defined? where did you get it from? – NickD Mar 17 '21 at 05:00string-to-number
. – George Mar 17 '21 at 05:22