perl find and replace with single quotes

I need to find / replace the following string in all the .txt files in my working directory:

myfunc(l_define,'BASE TABLE')

I've tried the following, which runs without errors, but doesn't update the file(s).

perl -pi -w -e "s/myfunc(l_define,'BASE')/myfunc(l_define,'BASE',1,1,'')/g;" *.txt

Is it a quoting issue?

1 answer

  • answered 2017-06-17 18:41 LukStorms

    In a regex, what's between ( and ) is a capture group.

    So for the ( or ) characters you need to escape them, so that the literal characters are matched. So use \( and \) in the regex.

    The regex myfunc(l_define,'BASE') would only match strings that have myfuncl_define,'BASE'.

    Escaping is often needed to match characters that have a special meaning in the regex syntax:
    . ( ) [ ] + * ? { } ^ $ \ |
    Also / if the code uses something like s/a\/b/a-b/g instead of something like s@a/b@a-b@g

    And if you really don't like to use those backslashes?
    You could also use a character class: [(] and [)]