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

Hi Monks, I'm trying to extract data from a string and assign them to variables like so:

use Modern::Perl; my $string = " define('DB_NAME', 'blah'); /** MySQL database username */ define('DB_USER', 'blahbah'); /** MySQL database password */ define('DB_PASSWORD', 'blahblahblah'); "; my ($db, $db_user, $db_pass); my $pre_regex = qr/^\s*define\s*\(\s*['"]/; my $post_regex = qr/['"]\s*,\s*['"]([^'"]+)['"]/; ($db, $db_user, $db_pass) = map { ($string =~ /$pre_regex $_ $post_regex/mix || 'unknown') } q +w(DB_NAME DB_USER DB_PASSWORD); say $db; say $db_user; say $db_pass;
The result is this, though:
1 unknown unknown

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Assigning mapped regex to a list
by haukex (Archbishop) on Feb 27, 2017 at 20:54 UTC

    Two problems: First, note that each qr// has its own set of modifiers, so the /mix modifiers don't affect those regexes, and the ^ anchors to the beginning of the string, not each line. Second, the || operator places the =~ m// operation into scalar context, so it won't return the list of capture groups, just a true/false value for success/failure. The following works for me:

    my $pre_regex = qr/^\s*define\s*\(\s*['"]/mix; my $post_regex = qr/['"]\s*,\s*['"]([^'"]+)['"]/mix; ($db, $db_user, $db_pass) = map { $string =~ /$pre_regex $_ $post_regex/mix ? $1 : 'unknown' } qw(DB_NAME DB_USER DB_PASSWORD);

    However, having said that, I don't quite understand this approach. What is this you're trying to parse, some kind of config file? Parsing parenthesized and quoted stuff can easily become nontrivial (escaping, nesting, etc.), and this regex approach seems a little too simplistic.

      What is this you're trying to parse, some kind of config file?

      It is a fragment of a wordpress/wp-admin/setup-config.php file

      It is a fragment of a wordpress/wp-config.php file, uggg

        It is a fragment of a \wordpress\wp-config.php file

        Ah, ok, that makes a bit more sense. Using Perl to parse PHP... fun ;-) I don't know much about WordPress so I don't know if there might be a better way to get at these values, but I can see how a hack like this might be acceptable/necessary, as long as it's just those three variables and not too much more. Of course, if whoever edits the config file uses any more complicated PHP syntax or expressions, the above regex will break.

        I wish i knew why i am only seeing my typos when i look at the posted page, and not in the preview or edit box

        in this case i hilited the wrong file before clicking on save-results in agent ransack. saw it clear as day in the post.

      Ah, of course, the qr operator actually compiles the regex so it makes sense they get their own modifiers. And I never would have figured out the || operator throwing into scalar. Perfect. Thanks so much.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        And thank you for not telling anyone what you are trying to do with this code.
      "I don't quite understand this approach."

      And this is why I did not answer the OP's question. Not explaining what his goal is shows that the OP himself probably does not know what he is doing at all.

      He is trolling you, you see. Wasting your time.

Re: Assigning mapped regex to a list
by Anonymous Monk on Feb 27, 2017 at 20:40 UTC
    Why are you even doing this? What is your end goal?