Detecting success on partial match with capture? 
Author Message
 Detecting success on partial match with capture?

I have input (similiar to an INI file) of the format:

key
key value
key value extra value
key = value
key=value, value, and more value

   Note that lines may have a key or key + value, and that the '='
delimiter is optional (ackphffft!).  I'm trying to capture using:

  my ($Key,$Val) = /^(\S+)[\s=]+(.*)/ ;

  Of course, this only assigns the variables if both key & value are
detected.  If the regex fails (like key-only lines) it returns undef.
I would like to do a "partial capture" without resorting to:
   $Key = $1 ;
   $Val = $2 ;



Fri, 24 Sep 2004 23:34:16 GMT  
 Detecting success on partial match with capture?

Quote:

> I have input (similiar to an INI file) of the format:

> key
> key value
> key value extra value
> key = value
> key=value, value, and more value

look at the code in ConfigReader::Simple, which handles all of those cases:

http://search.cpan.org/search?mode=module&query=ConfigReader%3A%3ASimple

--

The Perl Review - a new magazine devoted to Perl
<http://www.theperlreview.com>



Sat, 25 Sep 2004 00:06:26 GMT  
 Detecting success on partial match with capture?

Quote:

> > I have input (similiar to an INI file) of the format:

> > key
> > key value
> > key value extra value
> > key = value
> > key=value, value, and more value

> look at the code in ConfigReader::Simple, which handles all of those cases:

> http://search.cpan.org/search?mode=module&query=ConfigReader%3A%3ASimple

   That will work.  I was just hoping there was a clever 1-liner floating around.


Sat, 25 Sep 2004 17:01:33 GMT  
 Detecting success on partial match with capture?

[snip]

Quote:

> look at the code in ConfigReader::Simple, which handles all of those cases:

> http://search.cpan.org/search?mode=module&query=ConfigReader%3A%3ASimple

   I'm probably missing something, but after some tests, it appears
that the regex ( /^\s*(\w+)\s+(['"]?)(.*?)\2\s*$/ ) in
ConfigReader::Simple::parse_line won't properly handle cases like:

   A=B
   A = B

   The EQUALS-SIGN leaks into the 'value' portion ($3).  But this
works:

   if (my ($key,$val) = (^\s*(\w+)[\s=]*(['"]?)(.*?)\2\s*$/)[0,2])
   {
      ... do thangs ....
   }



Sat, 25 Sep 2004 17:47:30 GMT  
 Detecting success on partial match with capture?

Quote:

> I have input (similiar to an INI file) of the format:
> key
> key value
> key value extra value
> key = value
> key=value, value, and more value
> Note that lines may have a key or key + value, and that the '='
> delimiter is optional

Are you suggesting a reader is not sufficiently savvy to notice this?
You are not sufficiently savvy to indicate a space or spaces may be
included as a delimiter or as a part of a delimiter character set.

Clearly my savvy level is sufficient to notice what you do not.

Quote:
> I'm trying to capture using:
>   my ($Key,$Val) = /^(\S+)[\s=]+(.*)/ ;
>   Of course, this only assigns the variables if both key & value are
> detected.  If the regex fails (like key-only lines) it returns undef.
> I would like to do a "partial capture" without resorting to:
>    $Key = $1 ;
>    $Val = $2 ;

Does use of default regex variables affect script performance?

Written per your parameters, a test script follows my signature.

Godzilla!
--
#!perl

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

while (<DATA>)
 {
  ($key, $value) = /^(\w+)[ =\n]*(.*)/;

  if (($key) && ($value))
   { print "Key: $key  Value: $value\n"; }
  elsif (($key) && (!($value)))
   { print "Key: $key    *  : No Value Found\n"; }
  else
   { print " *   Key Missing At Line $.\n"; }
 }

__DATA__
key
key value

key value extra value
key = value
    = value
key=value, value, and more value

PRINTED RESULTS:
________________

Key: key    *  : No Value Found
Key: key  Value: value
 *   Key Missing At Line 3
Key: key  Value: value extra value
Key: key  Value: value
 *   Key Missing At Line 6
Key: key  Value: value, value, and more value



Sat, 25 Sep 2004 18:06:01 GMT  
 Detecting success on partial match with capture?

Quote:


[snip]

> Are you suggesting a reader is not sufficiently savvy to notice this?
> You are not sufficiently savvy to indicate a space or spaces may be
> included as a delimiter or as a part of a delimiter character set.

> Clearly my savvy level is sufficient to notice what you do not.

   Miscommunication is the pervasive bane of my existence.  It invades
my language and disarms my erudition; with tentacles of distortion, it
ensnares the train of my thought and derails my meaning; it seeps into
the liquid vision of my consciousness and clouds intent; it warps my
words at a quantum level.   For example, someone could assume that my
original post was an attack on their interpretive ability, when all I
really wanted to do was Be Very Clear.  I apologize profusely.

Quote:
> > I'm trying to capture using:

> >   my ($Key,$Val) = /^(\S+)[\s=]+(.*)/ ;

> >   Of course, this only assigns the variables if both key & value are
> > detected.  If the regex fails (like key-only lines) it returns undef.
> > I would like to do a "partial capture" without resorting to:
> >    $Key = $1 ;
> >    $Val = $2 ;

> Does use of default regex variables affect script performance?

   A good question, but I'm not sure about what you mean by "default
regex variables".  Does that mean THE default variable $_, or the
"capturing" variables $1, $2, $3, etc.  If the second case, I believe
the answer is "yes, and negatively".

Quote:

> Written per your parameters, a test script follows my signature.

> Godzilla!
> --
> #!perl

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

> while (<DATA>)
>  {
>   ($key, $value) = /^(\w+)[ =\n]*(.*)/;

   The "\n" hadn't occured to me, but it seems to make sense.  Thx.

[snip]



Sat, 25 Sep 2004 23:56:00 GMT  
 Detecting success on partial match with capture?

(snipped)

Quote:
> Miscommunication is the pervasive bane of my existence.  It invades
> my language and disarms my erudition; with tentacles of distortion, it
> ensnares the train of my thought and derails my meaning; it seeps into
> the liquid vision of my consciousness and clouds intent; it warps my
> words at a quantum level.  

You win this paragraph as a prize found at the bottom
of a box of Cracker Jacks? Certainly a classic worthy
of this English teacher's Circular Filing Cabinet.

Quote:
> > > I would like to do a "partial capture" without resorting to:
> > >    $Key = $1 ;
> > >    $Val = $2 ;
> > Does use of default regex variables affect script performance?
> I believe the answer is "yes, and negatively".

Honestly and truly? I am certain you would not announce
this notion in lieu of careful testing.

   ($key, $value) = /^(\w+)[ =\n]*(.*)/;

You think there is a chance  $1   and   $2   are present
in my code although not included in my syntax?

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,
  {
   'Godzilla_1' =>
   '$input = "key value";
    ($key, $value) = $input =~ /^(\w+)[ =\n]*(.*)/;',

   'Godzilla_2' =>
   '$input = "key value";
    $input =~ /^(\w+)[ =\n]*(.*)/;
    $key = $1; $value = $2;',
  } );
 }

PRINTED RESULTS:
________________

Run One:

Benchmark: timing 100000 iterations of Godzilla_1, Godzilla_2...


Run Two:

Benchmark: timing 100000 iterations of Godzilla_1, Godzilla_2...


Run Three:

Benchmark: timing 100000 iterations of Godzilla_1, Godzilla_2...




Sun, 26 Sep 2004 03:44:35 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Partial Success installing Perl on Solaris 2.2

2. Partial matching of regexps

3. Partial matching of a regular expression

4. Finding partial match

5. Matching and capturing wide character strings in binary files

6. String decomposition/ capture part not matched by regex

7. Capturing matches from several regexpes

8. Match/capture question

9. regexp- capturing matched pattern with an alternation character

10. Capturing result of an unordered, multi-word match

11. image capture from video capture card

12. partial buffered writes

 

 
Powered by phpBB® Forum Software