Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Regex question. Oh I so cannot do regular expression matching. (http://www.velocityreviews.com/forums/t910232-regex-question-oh-i-so-cannot-do-regular-expression-matching.html)

grocery_stocker 04-14-2009 04:08 PM

Regex question. Oh I so cannot do regular expression matching.
 
I can't seem to get perl to match both the words 'chad' AND 'party'
in the string "chad ttyp0 party". Below is what I attempted.

[cdalten@localhost oakland]$ more match.pl
#!/usr/bin/perl
use warnings;

#$string = `w | grep cdalten | grep telnet`;

$test = "chad ttyp0 party";

if ("$test" =~/(\bchad\b)(\bparty\b)/) {
print "true \n";
}
[cdalten@localhost oakland]$ ./match.pl
[cdalten@localhost oakland]$

What am I doing wrong>

perl Newbie 04-14-2009 04:19 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
On Apr 14, 9:08*pm, grocery_stocker <cdal...@gmail.com> wrote:
> I can't seem to get perl to match both the words 'chad' AND *'party'
> in the string "chad ttyp0 party". Below is what I attempted.
>
> [cdalten@localhost oakland]$ more match.pl
> #!/usr/bin/perl
> use warnings;
>
> #$string = `w | grep cdalten | grep telnet`;
>
> $test = "chad ttyp0 party";
>
> if ("$test" =~/(\bchad\b)(\bparty\b)/) *{
> * * print "true \n";}
>
> [cdalten@localhost oakland]$ ./match.pl
> [cdalten@localhost oakland]$
>
> What am I doing wrong>


Add OR condition in your code

if ("$test" =~/(\bchad\b) || (\bparty\b)/)



perl Newbie 04-14-2009 04:25 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
On Apr 14, 9:08*pm, grocery_stocker <cdal...@gmail.com> wrote:
> I can't seem to get perl to match both the words 'chad' AND *'party'
> in the string "chad ttyp0 party". Below is what I attempted.
>
> [cdalten@localhost oakland]$ more match.pl
> #!/usr/bin/perl
> use warnings;
>
> #$string = `w | grep cdalten | grep telnet`;
>
> $test = "chad ttyp0 party";
>
> if ("$test" =~/(\bchad\b)(\bparty\b)/) *{
> * * print "true \n";}
>
> [cdalten@localhost oakland]$ ./match.pl
> [cdalten@localhost oakland]$
>
> What am I doing wrong>


You can use OR , AND operator as per your requirement

1. if ("$test" =~/(\bchad\b)/ || "$test" =~/(\bparty\b)/ ) {
2. if ("$test" =~/(\bchad\b)/ && "$test" =~/(\bparty\b)/ ) {



Darren Dunham 04-14-2009 04:27 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
grocery_stocker <cdalten@gmail.com> wrote:
> I can't seem to get perl to match both the words 'chad' AND 'party'
> in the string "chad ttyp0 party". Below is what I attempted.
>
> [cdalten@localhost oakland]$ more match.pl
> #!/usr/bin/perl
> use warnings;
>
> #$string = `w | grep cdalten | grep telnet`;
>
> $test = "chad ttyp0 party";
>
> if ("$test" =~/(\bchad\b)(\bparty\b)/) {


Why are you quoting $test?

This will only work if there were a string with "chad" and "party" with
nothing between them but a wordbreak. Such a string doesn't exist
(because you'd need a character to create the wordbreak).

So you probably want either...

if ($test =~/(\bchad\b)/ and
$test =~/(\bparty\b)/) {

(But that won't preserve $1 properly if you want to capture both items)

or

if ($test =~ /(\bchad\b).*(\bparty\b)/) {

because there are actually characters between them.

--
Darren

Uri Guttman 04-14-2009 04:27 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
>>>>> "pN" == perl Newbie <Ansher.M@gmail.com> writes:

pN> On Apr 14, 9:08*pm, grocery_stocker <cdal...@gmail.com> wrote:
>> I can't seem to get perl to match both the words 'chad' AND *'party'
>> in the string "chad ttyp0 party". Below is what I attempted.
>>
>> [cdalten@localhost oakland]$ more match.pl
>> #!/usr/bin/perl
>> use warnings;
>>
>> #$string = `w | grep cdalten | grep telnet`;
>>
>> $test = "chad ttyp0 party";
>>
>> if ("$test" =~/(\bchad\b)(\bparty\b)/) *{


perldoc -q var

don't quote scalar vars

>> * * print "true \n";}
>>
>> [cdalten@localhost oakland]$ ./match.pl
>> [cdalten@localhost oakland]$
>>
>> What am I doing wrong>


pN> Add OR condition in your code

pN> if ("$test" =~/(\bchad\b) || (\bparty\b)/)

huh? have you tried that yourself? also the OP wanted both words to
match and your attempt implies OR. besides there is no boolean logic
INSIDE regexes.

but i have several questions for the OP. why are you grabbing both words
when you only print true if you found them? if that is all you want then
an external boolean test with two separate regexes:

if ( $test =~ /\bchad\b/ && $test =~ /\bparty\b/ ) {

if you want that in one regex you need to account for text between the
two words:

if ( $test =~ /\bchad\b.*\bparty\b/ ) {

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Jürgen Exner 04-14-2009 04:30 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
grocery_stocker <cdalten@gmail.com> wrote:
>I can't seem to get perl to match both the words 'chad' AND 'party'
>in the string "chad ttyp0 party". Below is what I attempted.


>$test = "chad ttyp0 party";
>if ("$test" =~/(\bchad\b)(\bparty\b)/) {


Why are you quoting $test? Please see 'perldoc -q quoting':
What's wrong with always quoting "$vars"?

Your RE is trying to match a word boundary, followed by 'chad', followed
by a word boundary, immediately followed by a word boundary, then
'party', then another word boundary.

Obviously your test data contains other characters between the word
boundarybehind 'chad' and the word boundaryin front of 'party',
therefore it cannot match.

Depending upon what you want to achive you can either split the RE into
two
if ($test =~/\bchad\b/ and $test =~/\bparty\b/)
or insert some RE between those two word boundaries that will suck up
the additional characters, e.g.
if ($test =~/(\bchad\b).*(\bparty\b)/)

jue

grocery_stocker 04-14-2009 04:39 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
On Apr 14, 9:27 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "pN" == perl Newbie <Anshe...@gmail.com> writes:

>
> pN> On Apr 14, 9:08 pm, grocery_stocker <cdal...@gmail.com> wrote:
> >> I can't seem to get perl to match both the words 'chad' AND 'party'
> >> in the string "chad ttyp0 party". Below is what I attempted.
> >>
> >> [cdalten@localhost oakland]$ more match.pl
> >> #!/usr/bin/perl
> >> use warnings;
> >>
> >> #$string = `w | grep cdalten | grep telnet`;
> >>
> >> $test = "chad ttyp0 party";
> >>
> >> if ("$test" =~/(\bchad\b)(\bparty\b)/) {

>
> perldoc -q var
>
> don't quote scalar vars
>
> >> print "true \n";}
> >>
> >> [cdalten@localhost oakland]$ ./match.pl
> >> [cdalten@localhost oakland]$
> >>
> >> What am I doing wrong>

>
> pN> Add OR condition in your code
>
> pN> if ("$test" =~/(\bchad\b) || (\bparty\b)/)
>
> huh? have you tried that yourself? also the OP wanted both words to
> match and your attempt implies OR. besides there is no boolean logic
> INSIDE regexes.
>
> but i have several questions for the OP. why are you grabbing both words
> when you only print true if you found them? if that is all you want then
> an external boolean test with two separate regexes:
>
> if ( $test =~ /\bchad\b/ && $test =~ /\bparty\b/ ) {
>
> if you want that in one regex you need to account for text between the
> two words:
>
> if ( $test =~ /\bchad\b.*\bparty\b/ ) {
>


The question stems from a much larger side/site specific project that
I'm working on. I just couldn't figure out how to search for multiple
words in a single line. I figured it would have been just easier to
post the part of the code that was giving me grief.

grocery_stocker 04-14-2009 04:44 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
On Apr 14, 9:27 am, ddun...@taos.com (Darren Dunham) wrote:
> grocery_stocker <cdal...@gmail.com> wrote:
> > I can't seem to get perl to match both the words 'chad' AND 'party'
> > in the string "chad ttyp0 party". Below is what I attempted.

>
> > [cdalten@localhost oakland]$ more match.pl
> > #!/usr/bin/perl
> > use warnings;

>
> > #$string = `w | grep cdalten | grep telnet`;

>
> > $test = "chad ttyp0 party";

>
> > if ("$test" =~/(\bchad\b)(\bparty\b)/) {

>
> Why are you quoting $test?
>


Because in the full size script, $test is actually...

$test = `w | grep cdalten | grep party`;

> This will only work if there were a string with "chad" and "party" with
> nothing between them but a wordbreak. Such a string doesn't exist
> (because you'd need a character to create the wordbreak).
>
> So you probably want either...
>
> if ($test =~/(\bchad\b)/ and
> $test =~/(\bparty\b)/) {
>
> (But that won't preserve $1 properly if you want to capture both items)
>
> or
>
> if ($test =~ /(\bchad\b).*(\bparty\b)/) {
>
> because there are actually characters between them.
>



Uri Guttman 04-14-2009 04:47 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
>>>>> "gs" == grocery stocker <cdalten@gmail.com> writes:

gs> On Apr 14, 9:27 am, ddun...@taos.com (Darren Dunham) wrote:
>>
>> Why are you quoting $test?
>>


gs> Because in the full size script, $test is actually...

gs> $test = `w | grep cdalten | grep party`;

so?? perl isn't the shell so it doesn't need quoting around single
scalars even if they have blanks in them.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

grocery_stocker 04-14-2009 05:58 PM

Re: Regex question. Oh I so cannot do regular expression matching.
 
On Apr 14, 9:47 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "gs" == grocery stocker <cdal...@gmail.com> writes:

>
> gs> On Apr 14, 9:27 am, ddun...@taos.com (Darren Dunham) wrote:
> >>
> >> Why are you quoting $test?
> >>

>
> gs> Because in the full size script, $test is actually...
>
> gs> $test = `w | grep cdalten | grep party`;
>
> so?? perl isn't the shell so it doesn't need quoting around single
> scalars even if they have blanks in them.
>


Actually, the script borks when I try to use backticks.

[cdalten@localhost oakland]$ more match.pl
#!/usr/bin/perl
use warnings;

#$string = `w | grep cdalten | grep telnet`;

$test = `w | grep cdalten | grep telnet`;

print $test;

if ($test =~/(\bchad\b)/ && $test =~/(\btelnet\b)/ ) {
print "true \n";
}
[cdalten@localhost oakland]$ ./match.pl
cdalten pts/7 :0.0 Mon12 14:55 0.62s 0.24s telnet
[cdalten@localhost oakland]$

Ides why?


All times are GMT. The time now is 02:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.