help with pattern match (repeated patterns) 
Author Message
 help with pattern match (repeated patterns)

I can't figure out this pattern match.regexp :-(

One line consists of several fields of the form

        Key: Value

where both key and value are simple ascii test *which can contain
spaces*. Both key and value are also separated by spaces.

I do have a list of all possible keys. I would like to find all
key/value pairs in the line and assign them to a hash.

example:

#known keywords:
$kw="user|port|protocol|session|access|event|bytes sent|bytes received|reason";

#our string:
$msg = "event: Log out  port: 14        user: rapple    bytes sent: 1093501     bytes received: 181501";

%w = ($msg =~ /($kw):\s+(.+?)\s+(?:$kw|$)?/g);
               _____    _____   _________
                 |        |      lookahead whether followed by another keyword (or EOL)
                 |        the value
                 the keyword (using the Alternation)

# print out
for (($k,$v) = each %w) { print "$k=$v\n"; }

yields:

bytes sent=1093501
bytes sent=1093501

when I expected:

event=Log out
port=14
user=rapple
bytes sent=1093501
bytes received=181501

What am I doing wrong?? Help please !!!

regards
Mathias

--


Q'town Tel. Exch. | PGP: Keyid: 768/25E082BD
2 Stirling Rd     |      1A 8B FC D4 93 F1 9A FC BD 98 A3 1A 0E 73 01 65
S'pore 148943     | Disclaimer: I speak only for myself
* Eifersucht ist eine Leidenschaft, die mit Eifer sucht, was Leiden schafft *



Sun, 26 Jul 1998 03:00:00 GMT  
 help with pattern match (repeated patterns)

Quote:

>I can't figure out this pattern match.regexp :-(

>One line consists of several fields of the form

>    Key: Value

>where both key and value are simple ascii test *which can contain
>spaces*. Both key and value are also separated by spaces.

>I do have a list of all possible keys. I would like to find all
>key/value pairs in the line and assign them to a hash.

>What am I doing wrong?? Help please !!!

Perhaps this would work better with a split:

# known keywords:
$kw="user|port|protocol|session|access|event|bytes sent|bytes received|reason";

# our string:
$msg = "event: Log out  port: 14        user: rapple    bytes sent: 1093501     bytes received: 181501";


while (($k,$v) = each %kw) { print "$k=$v\n"; }

Of course, it's not a one-liner (you've gotta shift that first null off of
the array), but Randal might be willing to add his tuppence ;-).

Hope that helps you,


  Human time is measured in units of human change.  So the most flagrant
  time-wasting may minimize change and thus conserve time.
    -- William Burroughs, in "The Place of Dead Roads"



Mon, 27 Jul 1998 03:00:00 GMT  
 help with pattern match (repeated patterns)
: I can't figure out this pattern match.regexp :-(

Me either. But I notice that there are always at least two spaces
between each key/value pair. You can use this info (If it is _always_
true, and not just a feature of the example you provided)

: One line consists of several fields of the form

:       Key: Value

: where both key and value are simple ascii test *which can contain
: spaces*. Both key and value are also separated by spaces.

: I do have a list of all possible keys. I would like to find all
: key/value pairs in the line and assign them to a hash.

: example:

: #known keywords:

My approach also does not need to know the keywords...

: #our string:
: $msg = "event: Log out  port: 14        user: rapple    bytes sent: 1093501     bytes received: 181501";

---------------------------
#!/usr/bin/perl -w

#our string:
$msg = "event: Log out  port: 14        user: rapple    bytes sent: 1093501
 bytes received: 181501";


   ($k, $w) = split(/: /);  # split key from value
   $w{$k} = $w;

Quote:
}

# print out
foreach (keys %w) { print "$_=", $w{$_}, "\n"; }
---------------------------

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  Interesting trivia: If you took all the sand in North Africa and spread
     it out... it would cover the Sahara desert.



Mon, 27 Jul 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Regular Expressions/Pattern Matching/Unordered pattern

2. pattern as a sentence in pattern matching

3. HELP: Need help with "pattern matching"

4. HELP: Pattern matching. - Thanks to all who helped :)

5. How do I look for repeated patterns

6. Repeated patterns

7. Finding repeating patterns inside arrays

8. repeat pattern in one line

9. repeating pattern

10. Need help on pattern matching

11. Need Help with Pattern Matching

12. pattern matching help

 

 
Powered by phpBB® Forum Software