Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Cannot get Perl to match a specific string in my textfile

by skasch (Novice)
on Jan 12, 2017 at 14:50 UTC ( [id://1179450]=perlquestion: print w/replies, xml ) Need Help??

skasch has asked for the wisdom of the Perl Monks concerning the following question:

Dear list,

I am a beginner with Perl and seek wisdom of the monks

What i want is to read a file, run a regex on its lines and when matching substitute some strings according to a map.

Mostly that does work but on a specific line, i cannot get my regex to match and I like to understand why

This is an excerpt of one of the files that should be processed

"user.name@domain.com:Calendar/personal" = <*I0>; }; SubscribedFolders = ( "user@domain.com:Calendar/BCA-513DD600-1B-6967B200" ); FoldersOrder = ( personal, "user_A_domain_D_com_BCA-513DD600-1B-6967B200", "7D03-5682B480-975-5FFE8000", "7D03-5682B480-977-5FFE8000" ); FreeBusyExclusions = { "user.name@domain.com:Calendar/personal" = <*I0>; "user@domain.com:Calendar/BCA-513DD600-1B-6967B200" = <*I1>;

And this is my Code

#!/usr/bin/perl use strict; use warnings; use autodie; my %replacements = ( 'user.name@domain.com' => 'uname', 'user@domain.com' => 'user', ); open( my $readFile, '<', "sampleFile" ); while ( <$readFile> ) { # if contains :Calendar and is suffixed with / # or :Contacts with same suffix or Users prefixed # with / or is an email-address followed by " = if ( m/:Calendar(?=\/)/, m/:Contacts(?=\/)/, m/(?<=\"\/)Users/, m/.+@.+\"\s=/) { # then replace every occurrence as in list foreach my $key ( sort keys %replacements ) { s/\b$key\b/$replacements{$key}/g; } } print $_; }

And this is the result

"uname:Calendar/personal" = <*I0>; }; SubscribedFolders = ( "user@domain.com:Calendar/BCA-513DD600-1B-6967B200" ); FoldersOrder = ( personal, "user_A_domain_D_com_BCA-513DD600-1B-6967B200", "7D03-5682B480-975-5FFE8000", "7D03-5682B480-977-5FFE8000" ); FreeBusyExclusions = { "uname:Calendar/personal" = <*I0>; "user:Calendar/BCA-513DD600-1B-6967B200" = <*I1>;

I do not understand why my regex does not match the string under "Subscribed Folders" any help is greatly appreciated

cheers, Sascha

Replies are listed 'Best First'.
Re: Cannot get Perl to match a specific string in my textfile
by hippo (Bishop) on Jan 12, 2017 at 15:01 UTC

    The comma doesn't do what you think. Replace it with || and it matches that line. SSCCE:

    #!/usr/bin/perl use strict; use warnings; use autodie; my %replacements = ( 'user.name@domain.com' => 'uname', 'user@domain.com' => 'user', ); $_ = ' "user@domain.com:Calendar/BCA-513DD600-1B-6967B200"'; #if (m/:Calendar(?=\/)/, m/:Contacts(?=\/)/, m/(?<=\"\/)Users/, m/.+@. ++\"\s=/) { if (m/:Calendar(?=\/)/ || m/:Contacts(?=\/)/ || m/(?<=\"\/)Users/ || m +/.+@.+\"\s=/) { print "Matched\n"; # then replace every occurrence as in list foreach my $key (sort keys %replacements) { s/\b$key\b/$replacements{$key}/g; } } print "$_\n";
Re: Cannot get Perl to match a specific string in my textfile
by skasch (Novice) on Jan 12, 2017 at 15:06 UTC

    Dear list, ok, seems that i got the concept wrong. With

    if ( m/:Calendar(?=\/)/ or m/:Contacts(?=\/)/ or m/(?<=\"\/)Users/ or m/.+@.+\"\s=/) {

    the regex works as intended. thanks, sascha

    and thanks to hippo!

Re: Cannot get Perl to match a specific string in my textfile
by Marshall (Canon) on Jan 13, 2017 at 00:56 UTC
    Hi Sascha skasch,

    Just to elaborate on the post from Hippo. When you use the comma operator, only the "truthfulness" of last comma part matters! Here is a simple example that attempts to demo that only the last comma part matters as far as the "if" is concerned although it does throw some warnings:

    #!/usr/bin/perl use strict; use warnings; my $x = 2; if ($x < 0, $x >1){print "example1: ok, x>1\n";} if ($x > 1, $x <0) { print "example2: x is <0\n"; } else { print "example2: x is not <0\n"; } __END__ Useless use of numeric lt (<) in void context at C:\Projects_Perl\test +ing\CommaTruth.pl line 8. Useless use of numeric gt (>) in void context at C:\Projects_Perl\test +ing\CommaTruth.pl line 15. example1: ok, x>1 example2: x is not <0
    A more real world example that you might see is something like this...a couple of actions happen, then a "truth" test happens.
    #!/usr/bin/perl use strict; use warnings; my $input; while ( (print "some prompt: "), $input=<STDIN>, $input !~ /^\s*q(uit) +\s*/i) { print "I'm looping until you type q or quit\n"; } print "loop exited\n";
    There are other ways to write this "while" condition. Typically this can be done with an "and" logical construction because print returns a true value. Here I am just demo'ing that all that matters is that final regex test of whether to quit or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179450]
Approved by Corion
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found