1

perl-rename 's/b(\d{2}A)/\1/' * finds and renames every part of any file with

  1. "b" followed by

  2. 2 digits followed by

  3. "A"

Is there an option or way to make the regular expression case-insensitive, i.e. also matches either an uppercase "B" for the first part and/or a lowercase "a" for the last part?

4
  • 1
    Can you provide some samples? its not clear what you mean by "an uppercase "A" for the first part and/or a lowercase "a" for the last part?"
    – Inian
    Commented May 2, 2020 at 10:30
  • Is [aA] what you're looking for?
    – Kusalananda
    Commented May 2, 2020 at 10:32
  • @Kusalananda Yes. The matching works then. If you provide your comment as an answer, I can mark it as solved.
    – henry
    Commented May 2, 2020 at 10:37
  • @Inian I made a mistake with the "uppercase "A"". It should have been "B" instead. I genuinely thought the enumerated list was self-explanatory.
    – henry
    Commented May 2, 2020 at 10:38

2 Answers 2

2

The s/// substitution command in Perl (and thus in the Perl rename command) has a flag /i to do case insensitive match. All you need to do is

perl-rename -n 's/b(\d{2}A)/\1/i' *.jpg

Remove the -n flag when you identify the files are renamed as expected. It only does a dry-run to show how the files will be eventually renamed.

To selectively allow case insensitive matching of one or several characters, use e.g. [aA] (will match either a or A).

2

While the /i modifier is the most common and well-known answer, it should also be said that this can be modified inside the regex itself.

/(?i)b(\d{2}A)/

This can be useful for dynamically generated patterns, patterns saved in files, etc. Also, it can be "turned off" either by limiting it to a group (capturing or otherwise):

/(?:(?i)b(\d{2}A)) Foo Bar/ # the Foo Bar portion is still case-sensitive

Or by the fact that it's a modal setting, so you can switch it back off with a negation:

/(?i)b(\d{2}A)(?-i) Foo Bar/ # again only Foo Bar is case sensitive
1
  • Also /sensitive(?i:insensitive)sensitive/ Commented Sep 28, 2022 at 16:07

You must log in to answer this question.

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