Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

simple regex question

by pipeops (Novice)
on Nov 26, 2010 at 13:05 UTC ( #873860=perlquestion: print w/ replies, xml ) Need Help??
pipeops has asked for the wisdom of the Perl Monks concerning the following question:

Thanks everyone for the responses! I am however trying to get values from an array which was pulled from a file in this format: FILE: test one two three four end And then I would like to assign everything (breakdown by \n)between the word test and end so array1 will be: one two three four open FILE, "testfile" or die "cannot open file! \n"; @array = <FILE> @array1 = @array=~ m/"match anything of word test, but before word end"; I will then need to match up all entries between array and array1 and display entries that match. Is this possible?

Comment on simple regex question
Re: simple regex question
by Anonymous Monk on Nov 26, 2010 at 13:10 UTC
Re: simple regex question
by phenom (Chaplain) on Nov 26, 2010 at 14:20 UTC

    You could try something like this:

    #!/usr/bin/perl use strict; use warnings; $|++; use 5.010; my $before = 'foo'; my $after = 'stuff'; my $pattern = 'blablah'; my $combined = $before . $pattern . $after; my @test = qw( blahblahblah stuffstuffstuff fooblablahstuff fooblahblahstuff onefooblablahstuff ); match($_) for(@test); sub match { my $test = shift; say "$test matches" if( $test =~ /$combined/o ); }
      $|++
      Why?

      Is there a reason to insert obscure, needless, lines in your program? Considering that all you're are doing is writing newline terminated strings to STDOUT, which already flushes on newlines by default, there isn't much of a reason to disable buffering.

      But if, for some reason, you want to disable buffering, why this syntax? What's wrong with a plain $| = 1;? You do realize that not everyone knows what the result of $|++;$|++;$|--;$|--; is?

        You're absolutely correct. It's in there because it's part of a template I use for everything I write. Just habit, I suppose.

        I also agree that $| = 1 is more legible. But people who know about removing obscure, needless lines from programs will also know what it means. This too is just another habit - which even perlcritic doesn't complain about.

        There are some caveats with buffering on STDOUT. The behavior changes if Perl figures that STDOUT has been redirected to something not a physical console. Consider:
        #!/usr/bin/perl -w use strict; for (0..200) { print "$_ buffered xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"; print STDERR "$_ unbuffered \n"; }
        When run from the command line, STDOUT is indeed unbuffered and you see alternating buffered/unbuffered statements. However for example if I run this in Textpad (my editor on Windows - I think similar to Notepad++) and have it capture the output, STDOUT is buffered. And the first buffered line is output after about 88 lines. I conclude that on my Windows system the buffer is about 8KB. So it depends upon where STDOUT is directed to.

Re: simple regex question
by JavaFan (Canon) on Nov 26, 2010 at 15:09 UTC
    Untested:
    my ($match) = $offbcimp =~ /test([^e]*(?:e(?!nd)[^e]*)*)end/;
Re: simple regex question
by Utilitarian (Vicar) on Nov 26, 2010 at 15:46 UTC
    Hi pipeops,

    You should follow the advice above, it makes your code readable and you had to preview before you could post, so you must have noticed something wrong.

    Any way what you need to do is to start matching at the first word, and match anything up to your pattern and match anything to the end word.

    By putting a capturing parenthesis around the pattern the pattern will be returned if assigned in a list context, putting a variable in parenthesis makes it a list of one and so the first captured value will be assigned to it.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: simple regex question
by biohisham (Priest) on Nov 26, 2010 at 15:49 UTC
    What is the nature of the pattern that you are looking for? is it numerical, alphabetical, alphanumerical, composed of special characters....etc ?

    Show an example of what you are trying to accomplish... check perlrequick, perlretut->looking ahead and behind, perlreref can be a useful quick reference too



    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
      Hello! I am trying to get values from an array which was pulled from a file in this format: FILE: test one two three four end And then I would like to assign everything (breakdown by \n)between the word test and end so array1 will be: one two three four open FILE, "testfile" or die "cannot open file! \n"; @array = <FILE> @array1 = @array=~ m/"match anything of word test, but before word end"; I will then need to match up all entries between array and array1 and display entries that match.

        posting same badly formatted question as answer again does not help at all, except to get a downvote :-(

Re: simple regex question
by pipeops (Novice) on Nov 30, 2010 at 15:39 UTC
    All, I ended up using the following method with push. Here it creates 4 arrays from 1 based on its initial character on each line(they are all the same) Thanks everyone for the help foreach $line(@exceptions) { if ($line =~ m/^n/) { $line =~ s/^n//s; push (@before, $line); } if ($line =~ m/^i/) { $line =~ s/^i//s; push (@implementation, $line); } if ($line =~ m/^b/) { $line =~ s/^b//s; push (@backup, $line); } if ($line =~ m/^o/) { $line =~ s/^o//s; push (@offline, $line); } }
      pipeops, glad to hear you have it working. But for future reference, please wrap your <code> in tags - it's MUCH easier to read:

      foreach $line (@exceptions) { if ( $line =~ m/^n/ ) { $line =~ s/^n//s; push( @before, $line ); } elsif ( $line =~ m/^i/ ) { $line =~ s/^i//s; push( @implementation, $line ); } elsif ( $line =~ m/^b/ ) { $line =~ s/^b//s; push( @backup, $line ); } elsif ( $line =~ m/^o/ ) { $line =~ s/^o//s; push( @offline, $line ); } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-11-24 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (147 votes), past polls