Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Regular Expression

by archimca (Novice)
on Feb 17, 2011 at 21:19 UTC ( [id://888801]=perlquestion: print w/replies, xml ) Need Help??

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

Here is the string

%MYBOARD{project1|nameofproject1 remarks="Good,Verygood,bext,excellent +" grade="7" teacher="Priyanka"}%

with the below mention code I can fetch the value of grade

$var =~ m/%MYBOARD{.*?grade=\"([^\"]*)"[^}]*}%

What would be the regular expression if i want to fetch the value of 'teacher'

Replies are listed 'Best First'.
Re: Regular Expression
by toolic (Bishop) on Feb 17, 2011 at 21:31 UTC
    use warnings; use strict; my $s = '%MYBOARD{project1|nameofproject1 remarks="Good,Verygood,bext, +excellent" grade="7" teacher="Priyanka"}%'; if ($s =~ / \b teacher = " ([^"]+) " /x) { print "$1\n"; } __END__ Priyanka
Re: Regular Expression
by Ratazong (Monsignor) on Feb 17, 2011 at 21:33 UTC

    You are thinking very complicated ;-)

    Try to find the string teacher, a =", as few chars as possible, and then again a "

    $var =~ /teacher="(.*?)"/;
    (untested)(update: tested; it works :-)

    HTH, Rata

    [update2: removed escaping the double quotes (as they are not regex metacharacters) - thanks johngg for the feedback!]
Re: Regular Expression
by NetWallah (Canon) on Feb 17, 2011 at 21:38 UTC
    Get Both: (Linux run):
    >: perl -e 'print qq|$_\n| for q~%MYBOARD{project1|nameofproject1 rema +rks="Good,Verygood,bext,excellent" grade="7" teacher="Priyanka"}%~=~m +/grade="([^"]+).+teacher="([^"]+)/' 7 Priyanka

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Heck, if you're going to do that, you might as well get it all:

      #!/usr/bin/perl -w use strict; $_ = '%MYBOARD{project1|nameofproject1 remarks="Good,Verygood,bext,exc +ellent" grade="7" teacher="Priyanka"}%'; my %map; $map{$1} = $2 while s/^.*? (\S+)="([^"]+)"//g; print "$_: $map{$_}\n" for keys %map;

      Output:

      grade: 7 teacher: Priyanka remarks: Good,Verygood,bext,excellent

      The above can also be spelled

      print map {"$1: $2\n" if /(\S+)="([^"]+)"/} split;

      but I suspect that this would be a bit much for the OP... :)

      -- 
      Education is not the filling of a pail, but the lighting of a fire.
       -- W. B. Yeats
Re: Regular Expression
by 0xbeef (Hermit) on Feb 17, 2011 at 22:17 UTC
    Monks are noble creatures and have basic ethical expectations of visitors at the Monastery. So at least try doing *something* before posting here, and at the very least make sure that the code that you post actually works.

    Hint 1: The code you claim will fetch a value won't compile.
    Hint 2: Try substitution.
    Hint 3: Read How do I post a question effectively?

Re: Regular Expression
by CountZero (Bishop) on Feb 18, 2011 at 11:21 UTC
    The following will get your fields and values into a hash in one line:
    use Modern::Perl; use Data::Dumper; my $text = '%MYBOARD{project1|nameofproject1 remarks="Good,Verygood,be +xt,excellent" grade="7" teacher="Priyanka"}%'; my %data = $text =~/ ([^=]+)="([^"]+)/g; say Dumper(\%data);
    Output:
    $VAR1 = { 'grade' => '7', 'teacher' => 'Priyanka', 'remarks' => 'Good,Verygood,bext,excellent' };

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found