5

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

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I thought I read that thoroughly, apparently I did not.
1
(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

1 Comment

That really doesn't make any sense. Why assign matched?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.