Cheeso
Cheeso

Reputation: 192467

emacs: is there a way to specify a case-insensitive search in a regexp?

In Javascript, I just need to append a i to the regexp to make it case-insensitive. In .NET, I use a RegexOptions.IgnoreCase.

I know about case-fold-search. What I want is to specify that behavior in the regex itself, as specified in an elisp program. Possible?

Upvotes: 13

Views: 3989

Answers (2)

ebpa
ebpa

Reputation: 1259

It may not be appropriate depending on the context the regular expression is being used, but I'll offer the following suggestion as an alternate strategy for case-insensitive matching: There is no need to make the regular expression itself case insensitive- if you can normalize the matched string to be case-insensitive. You can achieve an effectively case-insensitive search by normalizing the case of the string to match the case of your regular expression. For example, demonstrating with string-match-p:

(let* ((my/string "Foo Bar"))
  (string-match-p "foo" (downcase my/string)))
;; Returns 0 (indicating a match at the start of the string)

Upvotes: -1

offby1
offby1

Reputation: 6983

As someone said earlier, the way to control this is to bind case-fold-search. There is no way to specify the regex's case-sensitivity (or lack thereof) in the regex itself.

As it happens, regexp matching is already case-insensitive by default.

Upvotes: 9

Related Questions