Suppose you want to match the pattern /branch.\d+/. Well for postgres you need to pass that in as a string and every \ needs to be doubled to escape them because \ already has meaning. So you get 'branch.\\d+'. But in Perl \ is special, so to pass that into postgres you have to write 'branch.\\\\d+'.
If you are very sure that $root has no escape characters in it, you can write something like
and it should work. But be warned that interpolating variables directly into SQL is a serious security risk because it leads to the possibility of SQL injection attacks. So you are better off with something like:$sth_get_branch_list = $dbh->prepare(qq( SELECT branch FROM postgres_database WHERE tre +e = 'first_tree' AND branch ~ '$root.\\\\d+'; ));
and then passing $root into execute. This will still not do the right thing if $root has regular expression metacharacters in it, but at least it isn't a major security risk.$sth_get_branch_list = $dbh->prepare(q( SELECT branch FROM postgres_database WHERE tre +e = 'first_tree' AND branch ~ ? || '.\\\\d+'; ));
In reply to Re: Perl and PostgreSQL regex
by tilly
in thread Perl and PostgreSQL regex
by chrishowe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |