For the sake of an answer:
string-match
is meant for to be used with regexps.
(string-match REGEXP STRING &optional START)
Return index of start of first match for REGEXP in STRING, or nil.
Matching ignores case if ‘case-fold-search’ is non-nil.
If third arg START is non-nil, start search at that index in STRING.
For index of first char beyond the match, do (match-end 0).
‘match-end’ and ‘match-beginning’ also give indices of substrings
matched by parenthesis constructs in the pattern.
You can use the function ‘match-string’ to extract the substrings
matched by the parenthesis constructions in REGEXP.
Here are some examples
(string-match "\\(dog\\|cat\\)" "There is a dog.") ;; => 11
(string-match "\\(dog\\|cat\\)" "There are two cats here." 0) ;; => 14
(string-match "\\(dog\\|cat\\)" "There are two cats here." 15) ;; => nil
(string-match "\\(dog\\|cat\\)" "There are horses.") ;; => nil
Note that if you don't plan on using match-data
after using string-match
you should prefer to use string-match-p
which works just like string-match
except that it doesn't modify match-data
when run.
As other have said, in your case looking-at
would probably be a better choice anyway.
(looking-at REGEXP)
Return t if text after point matches regular expression REGEXP.
This function modifies the match data that ‘match-beginning’,
‘match-end’ and ‘match-data’ access; save and restore the match
data if you want to preserve them.
string-match
already uses regexps (ii) let-bindcase-fold-search
to nil around your call if you want case sensitivity (iii) uselooking-at
for matching text at point (iv) you can useregion-beginning
andregion-end
instead ofpoint
andmark
in case the latter is before the former (bonus point for making sureuse-region-p
is non-nil)C-h f
string-match
would tell you.