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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
A simple pattern match question

 



eddiepop
New User

Mar 11, 2016, 7:37 PM

Post #1 of 3 (4072 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?


BillKSmith
Veteran

Mar 11, 2016, 9:24 PM

Post #2 of 3 (4069 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.


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

Output:

Code
more text

Good Luck,
Bill


eddiepop
New User

Mar 12, 2016, 9:36 AM

Post #3 of 3 (4058 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