5

It may be novice question. I'm just trying to learn elisp regex, and can't get my head arround why below is matching.

Here, I'm trying to match with string that has only Uppercase letters, but below is clearly matching with the lowercase ones.

(string-match-p "^[A-Z]+$"       "ASDaF")        
(string-match-p "^[[:upper:]]+$" "ASDaF")
(string-match-p "^[[:upper:]]+$" "alllowercase")
;; all return 0 not nil

Also with M-x re-builder.

enter image description here

1 Answer 1

7

C-hf string-match-p:

string-match-p is a byte-compiled Lisp function in ‘subr.el’.

(string-match-p REGEXP STRING &optional START)

Same as ‘string-match’ except this function does not change the match data.

which takes you to:

string-match is a built-in function in `src/search.c'.

(string-match REGEXP STRING &optional START INHIBIT-MODIFY)

Return index of start of first match for REGEXP in STRING, or nil.
Matching ignores case if `case-fold-search' is non-nil.

and:

case-fold-search is a variable defined in `src/buffer.c'.

Its value is t

Non-nil if searches and matches should ignore case.

So what you want is:

(let (case-fold-search)
  (list (string-match-p "^[[:upper:]]+$" "alllowercase")
        (string-match-p "^[[:upper:]]+$" "ALLUPPERCASE")))

=> (nil 0)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.