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

Hey monks!

Oops. I asked the question amazingly poorly. I'm reading many lines in a text file of the format below. There is no convenience of CASE from which I can pluck. The constant that I can parse with is the positioning of the words that I capitalized for demo purposes. I'm thinking I have to say, "give me the characters between the third \ and the ", assming you just read thru the text "add vdisk", or something like that. Oh great, I just asked another question amazingly poorly...

add vdisk "\v disks\type 1\VDNAME" disk_group="\d gs\DISKGRP1" size=50 +0 ...

Thanks

Edit: g0n - Original content follows for context:

what's the best way to extract the CAPITAL text from the line below into variables ?

add vdisk "\v disks\type 1\VDNAME" disk_group="\d gs\DISKGRP1 size=500 ...

Thanks

20060512 Janitored by Corion: Added code tags, formatting

Replies are listed 'Best First'.
Re: newbie parse Q
by GrandFather (Saint) on May 11, 2006 at 20:51 UTC

    That depends a whole lot on what you want to achieve actually. Simplisticly you could use a regex to extract runs of uppercase letters. Something like:

    my $runs = $str =~ /([A-Z]+)/g;

    But one of your strings looks like it ought have digits on the end so you may want to augment that to:

    my $runs = $str =~ /([A-Z]+\d*)/g;

    That finds runs of upper case characters optionally followed by digits. But that may not be the end of the variations that you need to match so you may be better with a much fussier regex. For example:

    my @runs = $str =~ /".*?\\([^\\"]*)"[^"]*"[^A-Z]*((?:(?! size).)+)/g;

    DWIM is Perl's answer to Gödel
Re: newbie parse Q
by swampyankee (Parson) on May 11, 2006 at 22:34 UTC

    Could you give us a few lines you actually intend to process, and the output you would like to see?

    Using something like split, on the (stated) assumption that the field locations are fixed or that double quotes (") are present would probably break in the future.

    To give some (virtually untested!) code:

    #!perl use strict; use warnings; my $string = "add vdisk \"\\vdisks\\type 1\\vdname\" ". "disk_group=\"\\d gs\\diskgrp1\" size=500"; # split on quotes my @quotes = split(/\"/, $string); print "\@quotes:\n\t" . join("\n\t", @quotes) ."\n";; # @quotes should be 'add vdisk'. # '\\vdisks\\type 1\\vdname', # 'disk_group=', # '\\d gs\\diskgrp1\\', # 'size=500' # split the second element on backslashes... my @back = split(/\\/, $quotes[1]); print "\@back:\n\t" . join("\n\t",@back)."\n"; # and popout the last element... my $is_this_it = pop(@back); print $is_this_it,"\n";

    produces

    vdname
    Is this vaguely what you were looking for?

    (One of the regex wizards here could probably reduce that to 1 line; I could probably reduce it to about 3, but I was deliberately verbose.)

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele
Re: newbie parse Q
by jeffa (Bishop) on May 11, 2006 at 20:34 UTC

    I don't know about a single best way, but here's a pretty good way:

    my @var = $line =~ /([A-Z]+)/g;

    UPDATE: no blazar and ikegami -- he wrote something along the lines of "grab the capitolized letters" BEFORE he changed his question.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      "I capitalized for demo purposes" implies these won't always be capitalized.

      Hmmm, he wrote:

      give me the characters between the third \ and the ", assming you just read thru the text "add vdisk"

      IIUC, a stricter translation (also taking into account the example line of text) of that sentence to a match would be

      /add vdisk.*?(?:\\[^\\]*?){2}\\([[:alpha:]]+)\b/;