I am having trouble figuring out why am I getting the error defined in the title.
This is the line of code I'm inputting into the command line:
perl -pi -e 's/(\/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)' myfilepath
Basically, what I'm trying to do is go through a body of text, find all the URLS and append something to the end of the domain. For example: https://thisisalink.com/navigate/page <-- I want to ignore the ]
I keep getting this error when I run that code though:
Unmatched [ in regex; marked by <-- HERE in *)|[ <-- HERE A-Z0-9 &@#%=~_|5.030003)/gxi)/ at -e line 1, <> line 1.
How to fix this issue?
CodePudding user response:
$]
is a special variable that contains the current version of the Perl interpreter used. Hence, [A-Z0 9 &@#%=~_|$]
is interpolated as [A-Z0 9 &@#%=~_|5.032001
(on my Perl 5.32.1), and the opening [
is thus unmatched. To fix this, escape the $
using \$
:
[A-Z0 9 &@#%=~_|\$]
Similarly, earlier in the regex, you are using [...$?...]
, except that $?
is also a special variable containing The status returned by the last pipe close, backtick (``) command, successful call to wait() or waitpid(), or from the system() operator. This does not cause any error since it should be an integer, but it will no match either $
or ?
as you'd like. Once again, escape the $
using \$?
.
In general, when you want to match a literal $
, you should probably escape it.