Reputation: 972
I have a problem where I fail to get a match-string after a string-match. I think the string-match works, at least it returns non nil, but I get an error when I try to get the match-string. How should I do it?
The failing function:
(defun small-test ()
(string-match "\\([0-9]+\\)-v\\([0-9]+\\).txt" "2011-v9.txt")
(message (match-string 1))
)
Upvotes: 5
Views: 3088
Reputation: 7951
(defun small-test ()
(setq matched (string-match"\\([0-9]+\\)-v\\([0-9]+\\).txt" "2011-v9.txt"))
(message (match-string 1 "2011-v9.txt"))
)
that should work
Upvotes: 1
Reputation: 26104
From C-h f match-string, I suggest that you read the bottom line:
(match-string NUM &optional STRING)
Return string of text matched by last search. NUM specifies which parenthesized expression in the last regexp. Value is nil if NUMth pair didn't match, or there were less than NUM pairs. Zero means the entire text matched by the whole regexp or whole string. STRING should be given if the last search was by `string-match' on STRING.
Upvotes: 10