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

2

13

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?

Worker answered 31/5, 2010 at 15:29 Comment(0)
G
9

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.

Giraudoux answered 31/5, 2010 at 18:28 Comment(2)
I do not know about case-fold-search. Would you elaborate? I tried M-x customize-variable RET case-fold-search; I set it to nil. But, this still seems to be ignoring case: M-x search-forward-regexp RET ^[^^I]+^I[a-z]. Meanwhile, since almost eight years have passed, is there now a way to make an individual emacs regexp be case insensitive?Zarah
As far as I know, case-fold-search is still the only way to do it. Try using M-x set-variable RET instead of M-x customize-variable, and ensure that you do so in the same buffer in which you're doing the search.Giraudoux
A
-1

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)
Alonzoaloof answered 23/5 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.