CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Escaping a special variable embedded in a regex



Oct 19, 2017, 3:02 PM

Post #1 of 4 (5863 views)
Escaping a special variable embedded in a regex Can't Post

Although I have posted questions in the intermediate and advanced forums and I feel really stupid about this but this question really does belong in beginners. But here it is.

In preparing for a Perl test I encountered a pretty good exercise on this Wiley site It involves matching a bunch of strings against a bunch of regular expressions.

One of these patterns is:


Chasing a strange looking error, I put the whole list into an array of regexes looking like:

11 qr/^\w+$/,
12 qr/[^\w+$]/, <=== Note: In vim, the $] show up in white; can't mark that up here
13 qr/\d/,

When I run that do-nothing code I get this compile error:

$ ./ 
Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^\w+5.022004/ at ./ line 7.

I've attached a screen shot of the vim session. Notice the color change on line 12.

That 5.022004 gives me a broad hint: It happens to be the version of Perl I'm using. So the pattern contains an embedded special variable $] .

NOW I come to the question: How would I escape that special variable $] inside the regex pattern?

(If you thing this is verbose, be glad I realized the cause of the error before posting. Angelic)

Much thanks for an idea here!
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)
Attachments: just-patterns.png (14.6 KB)

Veteran / Moderator

Oct 19, 2017, 11:23 PM

Post #2 of 4 (5854 views)
Re: [rpaskudniak] Escaping a special variable embedded in a regex [In reply to] Can't Post

What do you want to match exactly with the:



There is most probably something wrong with it, but trying to fix it depends on the intended goal, which isn't clear to me.

If you want to match a literal [, then you need to escape it and it does not make sense to have a beginning of stgring anchor after it.

Square brackets introduce presumably a character class, but why would you want ^ or $ (beginning and end of string anchors) in a character class? It doesn't make much sense.

If you're looking for literal ^ and $ (sound unlikely, but who knows?), then I guess you would need to escape them (I can't test right now in public transportation toward my $work).

So, I would say, tell us what it is supposed to match, then we can think about how to fix it.


Oct 20, 2017, 4:11 AM

Post #3 of 4 (5851 views)
Re: [rpaskudniak] Escaping a special variable embedded in a regex [In reply to] Can't Post

I assume that you wish to match any character is which is neither a word-character nor a plus sign nor a dollar sign.

Here is one way to do that.

C:\Users\Bill\forums\guru>type rpaskudniak.t 
use strict;
use warnings;
use Test::More tests=>4;
my $class = q(^A-Za-z0-9_+$);
my $regex = qr/[$class]/;
my $string;

$string = q(b);
ok( $string !~ $regex, $_ );

$string = q(+);
ok( $string !~ $regex, $_ );

$string = q($);
ok( $string !~ $regex, $_ );

$string = q(This should match!);
ok( $string =~ $regex, $_ );

C:\Users\Bill\forums\guru>perl rpaskudniak.t
ok 1
ok 2
ok 3
ok 4

Note: Backslash does not work as an escape inside a character class.

UPDATED: Removed unnecessary slashes from matches.
Good Luck,

(This post was edited by BillKSmith on Oct 20, 2017, 4:49 AM)


Oct 23, 2017, 6:10 AM

Post #4 of 4 (5724 views)
Re: [rpaskudniak] Escaping a special variable embedded in a regex [In reply to] Can't Post

I just reread the site you linked to. It contains a link to a 'Hint/Solution'. That contains a link to a script which demonstrates how the authors intended the REGEXs to be used. I think it is a bad idea to treat a REGEX as a single quoted string, but it does avoid your problem.



sub check_one {
my $re = shift;
print "$ARG =~ $re";

Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives