Pattern matching, grabing everything right of the match 
Author Message
 Pattern matching, grabing everything right of the match

Hello all,

I have a script that posts data to a website and accepts the returning
HTLM.  I'm using iterations of the following to pull out values from
the return.  I have 19 values to extract.  The problem I am having is
some of the values are of variable length and words.  Is there a
"universal" replacement for "\w+.\w+." that will grab all characters
to the right of the pattern no matter how long or how many spaces
there are?


if($str =~/CurrentTicketStatus=(\w+.\w+.)/ ) {
    $its_status = $1;
    print "ITS STatus = $its_status\n";
    next;
  }

Quote:
}

Thanks in advance for your help!

Steve



Sun, 15 Feb 2004 17:20:05 GMT  
 Pattern matching, grabing everything right of the match

Quote:
> I have a script that posts data to a website and accepts the
> returning HTLM.  I'm using iterations of the following to pull out
> values from the return.  I have 19 values to extract.  The problem I
> am having is some of the values are of variable length and words.
> Is there a "universal" replacement for "\w+.\w+." that will grab all
> characters to the right of the pattern no matter how long or how
> many spaces there are?

".*" ?  Or maybe "[\w\s]*", depending on your need...

--
Ren Maddox



Sun, 15 Feb 2004 18:42:54 GMT  
 Pattern matching, grabing everything right of the match

Quote:

>Is there a
>"universal" replacement for "\w+.\w+." that will grab all characters

Yes:   .*

It is a little hard to believe that you didn't see that
yourself when you checked perlre.pod before posting...

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 15 Feb 2004 20:32:41 GMT  
 Pattern matching, grabing everything right of the match

Quote:

>I have a script that posts data to a website and accepts the returning
>HTLM.  I'm using iterations of the following to pull out values from
>the return.  I have 19 values to extract.  The problem I am having is
>some of the values are of variable length and words.  Is there a
>"universal" replacement for "\w+.\w+." that will grab all characters
>to the right of the pattern no matter how long or how many spaces
>there are?



>if($str =~/CurrentTicketStatus=(\w+.\w+.)/ ) {
>    $its_status = $1;
>    print "ITS STatus = $its_status\n";
>    next;
>  }
>}

i dont know what you are asking. if you want the rest of the line:

  if ($str =~/CurrentTicketStatus=(.*)$/ ) {

if you want something else, give us an example of the data,
and tell us what you want to be matched.

gnari



Mon, 16 Feb 2004 00:19:38 GMT  
 Pattern matching, grabing everything right of the match

Quote:

> Is there a "universal" replacement for "\w+.\w+." that will grab
> all characters to the right of the pattern no matter how long or
> how many spaces there are?


> if($str =~/CurrentTicketStatus=(\w+.\w+.)/ ) {
>     $its_status = $1;
>     print "ITS STatus = $its_status\n";
>     next;
>   }
> }

A presumption is made one or more characters will always
follow your key term. Whatever is there, or not there,
will be printed.

if (index ($str, "CurrentTicketStatus=") > -1)
 { print substr ($str, index ($str, "CurrentTicketStatus=") + 20); }

or if you like,

if (index ($str, "CurrentTicketStatus=") > -1)
 { print "ITS STatus = ", substr ($str, index ($str, "CurrentTicketStatus=") + 20); }

You don't need $its_status,

print "ITS STatus = $1 \n";

You don't need next; in your loop.

Godzilla!



Mon, 16 Feb 2004 01:11:19 GMT  
 Pattern matching, grabing everything right of the match

Quote:


> >Is there a
> >"universal" replacement for "\w+.\w+." that will grab all characters

> Yes:   .*

> It is a little hard to believe that you didn't see that
> yourself when you checked perlre.pod before posting...

Thanks! the .* worked great!


Mon, 16 Feb 2004 15:59:50 GMT  
 Pattern matching, grabing everything right of the match

Steve> Thanks! the .* worked great!

Presuming you have no newlines then.  For that, you either need
something easy to understand like /[\0-\377]*/, or hard to understand
like /(?s).*/ or /.*/s .

print "Just another Perl hacker," =~ /(?s)(.*)/;

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Mon, 16 Feb 2004 16:23:47 GMT  
 Pattern matching, grabing everything right of the match

(snipped)

Quote:
> > >Is there a
> > >"universal" replacement for "\w+.\w+." that will grab all characters
> > Yes:   .*
> Thanks! the .* worked great!

This syntax presented by many, this stereotypical
Perl 5 Cargo Cult code works, yes. However, your
proclaiming it "great" is certainly exaggeration.

Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

use Benchmark;

print "Run One:\n\n";
&Time;

print "\n\nRun Two:\n\n";
&Time;

print "\n\nRun Three:\n\n";
&Time;

sub Time
 {
  timethese (100000,
  {
   'name1' =>
   '$str = "CurrentTicketStatus=Godzilla Rocks And Rolls! Oh Yes! She Rocks My Socks Off!";
    if (index ($str, "CurrentTicketStatus=") > -1)
     { $output = substr ($str, index ($str, "CurrentTicketStatus=") + 20); }',

   'name2' =>
   '$str = "CurrentTicketStatus=Godzilla Rocks And Rolls! Oh Yes! She Rocks My Socks Off!";
    if ($str =~ /CurrentTicketStatus=(.*)/)
     { $output = $1; }',
  } );
 }

PRINTED RESULTS:
________________

Run One:

Benchmark: timing 100000 iterations of name1, name2...


Run Two:

Benchmark: timing 100000 iterations of name1, name2...


Run Three:

Benchmark: timing 100000 iterations of name1, name2...




Mon, 16 Feb 2004 17:03:09 GMT  
 Pattern matching, grabing everything right of the match

Quote:


> Steve> Thanks! the .* worked great!

> Presuming you have no newlines then.  For that, you either need
> something easy to understand like /[\0-\377]*/, or hard to understand
> like /(?s).*/ or /.*/s .

$ perl -le 'if("\x{20ac}" =~ /^[\0-\377]*\z/) { print "Match!" } else {
print "No match." }'
No match.
$ perl -le 'if("\x{20ac}" =~ /^.*\z/s) { print "Match!" } else { print
"No match." }'
Match!

Cheers,
Philip
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Wed, 18 Feb 2004 07:44:16 GMT  
 Pattern matching, grabing everything right of the match


Steve> Thanks! the .* worked great!

Quote:

>> Presuming you have no newlines then.  For that, you either need
>> something easy to understand like /[\0-\377]*/, or hard to understand
>> like /(?s).*/ or /.*/s .

Philip> $ perl -le 'if("\x{20ac}" =~ /^[\0-\377]*\z/) { print "Match!" } else {
Philip> print "No match." }'
Philip> No match.
Philip> $ perl -le 'if("\x{20ac}" =~ /^.*\z/s) { print "Match!" } else { print
Philip> "No match." }'
Philip> Match!

You and your big fat bytes... that's soooooo... Un-American!  I'm
going to report you to the Un-American Activities Committe immediately!

:-)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 18 Feb 2004 15:08:38 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Pattern bug matching whitespace in multi-line match?

2. Pattern Match - substitute a string after the match

3. combine two pattern matches to one match?

4. Slick way to return everything within matching parens

5. Regular Expressions/Pattern Matching/Unordered pattern

6. pattern as a sentence in pattern matching

7. help with pattern match (repeated patterns)

8. Matching ( Right to Left ) - Finding end of a String

9. Pattern matching: are regexes always fastest?

10. pattern matching with non-existence of a string

11. Pattern matching, global and returning list

12. pattern matching question

 

 
Powered by phpBB® Forum Software