Search code examples
regexunixpcreack

ack: Escape single quotes while providing regex


I am using ack to search for all values of width enclosed within single or double quotes.

Ex: width="23" , width='420'

RegEx Pal confirms /width=("|')\d+\1/g is the right regex for the job. I am however finding it difficult to supply this regex to ack.

<prompt>$ ack 'width=("|\')\d+\1' <--- Escaping the ' in the regex like\' did not help

<prompt>$ ack 'width=("|\\\')\d+\1' <--- Neither did this.

Any ideas?


Solution

  • "width=['\"]\d+['\"]"
    

    works for me. The square brackets define a set of characters containing ' (by itself) and \" (escaped because of the surrounding double-quotes).

    It also works with grep, but you need the -P option because \d+ is not part of standard grep regex.