fluca1978
fluca1978

Reputation: 4068

emacs regexp to match strings with first letter capitalized

In a lisp function I've got the following test with a regular expression that should match any string that starts with a capital letter:

(if (string-match "^[A-Z].+" my-string)

However this matches also the lower case starting strings. What am I missing here?

Upvotes: 3

Views: 402

Answers (2)

Stefan
Stefan

Reputation: 28611

Note that it's worse: it also matches "...\nHello" even though it starts with a dot, because ^ matches not just the beginning of the string, but also the beginning of any line inside that string. The regexp-operator that only matches the beginning of a string is \`. I recommend you use:

(let ((case-fold-search nil)) (string-match "\\`[[:upper:]]" my-string))

Upvotes: 1

Oleg Pavliv
Oleg Pavliv

Reputation: 21192

From string-match description ( to show it type C-h f or M-x describe-function ):

(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.

Just set case-fold-search to nil.

(let ((case-fold-search nil))
 (string-match "^[A-Z].+"  my-string))

Upvotes: 4

Related Questions