Re: simple regex question
by phenom (Chaplain) on Nov 26, 2010 at 14:20 UTC
|
#!/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 );
}
| [reply] [d/l] |
|
$|++
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?
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
Re: simple regex question
by Anonymous Monk on Nov 26, 2010 at 13:10 UTC
|
| [reply] |
Re: simple regex question
by JavaFan (Canon) on Nov 26, 2010 at 15:09 UTC
|
my ($match) = $offbcimp =~ /test([^e]*(?:e(?!nd)[^e]*)*)end/;
| [reply] [d/l] |
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."
| [reply] [d/l] |
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 .
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
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);
}
}
| [reply] |
|
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 );
}
}
| [reply] [d/l] |