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: Intermediate:
A simple pattern match question


New User

Mar 11, 2016, 7:37 PM

Post #1 of 3 (5100 views)
A simple pattern match question Can't Post

$t = "\|";
if ( $_ =~ s/([^$t]*)$t// ) {;}

My ancient Perl quick reference guide says that $' contains "the string following what was matched by the last pattern match."

However, after this line is executed, $' contains a leading tab character. What exactly is meant by "the last pattern match"? Shouldn't that encompass the tab character as well, leaving $' with no leading tab?


Mar 11, 2016, 9:24 PM

Post #2 of 3 (5097 views)
Re: [eddiepop] A simple pattern match question [In reply to] Can't Post

Your problem is not your understanding of $', but rather with the way that strings are interpolated into regular expressions. In your example, the string $t contains only a pipe (|) character. (The backslash is used to interpolate it into the string.) This is actually what you need inside the character class. The other reference to $t needs a backslash to be understood as a regular character. The easiest way to achieve this is to use \Q.

use strict; 
use warnings;
$_ = '||||any text|more text';
my $t = "\|";
if ( $_ =~ s/([^$t]+)\Q$t\E// ) {;}
print "$'\n";


more text

Good Luck,

New User

Mar 12, 2016, 9:36 AM

Post #3 of 3 (5086 views)
Re: [BillKSmith] A simple pattern match question [In reply to] Can't Post

Thank you. That's very helpful.


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

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