7

I'm trying to use a regular expression in the man page of Bash by using less.

I press / in less to enter a pattern, and I type z and press the Enter. I expected it to not match upper-case z (Z), but it does.

How do I make it not match Z? What kind of regular expressions are these that are not case sensitive?

1

2 Answers 2

4

Pretty sure you can get around that by using -i or +i in order to set less to default.

0
3

It's explained in the man page for less.

The default action for REs is to ignore case if there are no uppercase characters present, but to act case-sensitively otherwise.

There are three modes available within less:

  1. Case context dependent: a search or RE without uppercase characters is considered to be case-insensitive, but a search or RE containing at least one uppercase character is considered to be case-sensitive. Examples: abc will match abc and aBC, but aBc will only match aBc and not abc or ABC. This is the default setting.
  2. Case sensitive: a search or RE pays full regard to the case of any letter. Example: abC will match only abC and not abc or ABC.
  3. Case insensitive: a search or RE pays no regard to the case of any letter. Example: abC will match any of abc, abC, or ABC.

You can toggle case sensitive comparisons with -I, and case context sensitive comparisons with -i.

The control can be specified in three ways:

  • On the command line, for example less -I bigfile.txt.
  • In the environment, for example export LESS=-i and later less bigfile.txt.
  • Within less itself, for example by starting less bigfile.txt and then typing -i.
4
  • Hey, roaima. I think this is incorrect. ``` -i or --ignore-case Causes searches to ignore case; that is, uppercase and lowercase are considered identical. This option is ignored if any uppercase letters appear in the search pattern; in other words, if a pattern contains uppercase letters, then that search does not ignore case. -I or --IGNORE-CASE Like -i, but searches ignore case even if the pattern contains uppercase letters. ``` From my experiments, it behaves as -I, not as -i.
    – regex
    Apr 10, 2019 at 23:14
  • 1
    You could also append |$A (which translates as or A after the end of the line) for your pattern to become case sensitive without having to the change the settings. Apr 11, 2019 at 16:35
  • @StéphaneChazelas that's thrown me. I had thought $ was only special at the end of an RE (or RE clause), so $A would match a dollar and a capital letter, but A$ would match a capital at the end of a line. (Update: I can't even mimic this with perl, but definitely works with less.) Apr 11, 2019 at 16:50
  • 1
    @roaima, it's different between BRE and ERE, POSIX requires BRE $a to match on $a, and ERE $a to not match ($ to match the end of the subject wherever it's found). In Perl/PCRE, $ matches at the end of the subject or before a trailing line delimiter at the end of the subject, or if the m flag is enabled ((?m)) at the end of the subject or before any line delimiter in the subject. Apr 11, 2019 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .