Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match-string in elisp not returning string matched by string-match

Tags:

regex

elisp

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))
  )
like image 596
ygram Avatar asked Dec 29 '22 00:12

ygram


1 Answers

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.

like image 59
Lindydancer Avatar answered Apr 06 '23 00:04

Lindydancer