in reply to splitting lspci output

What do you actually want to achieve? Show us an example of the result you require because our mind reading units tend to be a bit flakey and don't always let us know what it is that you want.

It may be that you want this:

use strict; use warnings; while (<DATA>) { chomp; next if ! length; my ($section, $device, $culprit, $rev) = m/(\S+)\s+ ([^:]+):\s+ ((?:(?!\s*\(rev).)+)\s* (.*)/x; next if ! defined $rev; print "$section, $device, $culprit, $rev\n"; } __DATA__ 00:1f.3 SMBus: Intel Corporation 82801BA/BAM SMBus (rev 12) 00:1f.4 USB Controller: Intel Corporation 82801BA/BAM USB (Hub #2) (re +v 12) 00:1f.5 Multimedia audio controller: Intel Corporation 82801BA/BAM AC' +97 Audio (rev 12)

Prints:

00:1f.3, SMBus, Intel Corporation 82801BA/BAM SMBus, (rev 12) 00:1f.4, USB Controller, Intel Corporation 82801BA/BAM USB (Hub #2), ( +rev 12) 00:1f.5, Multimedia audio controller, Intel Corporation 82801BA/BAM AC +'97 Audio, (rev 12)

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: splitting lspci output
by Anonymous Monk on May 18, 2006 at 04:02 UTC
    hi

    m/(\S+)\s+ ([^:]+):\s+ ((?:(?!\s*\(rev).)+))\s* (.*)/x;
    what does the above pattern match do? what is that 'x' stands for ? could you please explain ?

      The x option indicates that most white space should be ignored. That allows me to use spaces to break up the regex a little to make it easier to see where each field is.

      The first field is captured by (\S+)\s+ which grabs the first sequence of non-space characters from the start of the line.

      The second field is captured by ([^:]+):\s+ which matches and captures a sequence of characters excluding ':', then matches the ':' and any following white space.

      The third field is a little more interesting. ((?:(?!\s*\(rev).)+))\s* crawls along the remainder of the line matching and capturing one character at a time until it comes to '(rev' preceeded by any amount of white space. The (?:...) bit groups some stuff without capturing. The (?!...) bit looks ahead to make sure the following stuff doesn't match whatever '...' is. The '...' bit inside of (?:...) matches a single character so long as it's not the start of something that would match '\s*\(rev'.

      The fourth field just picks up everything from '(Rev' to the end of the line.


      DWIM is Perl's answer to Gödel
        In other words:
        m/ ^(\S+) # Grab all non white space at the start into $1 \s+ # Skip any white space ([^:]+) # Now grab everything that isn't a colon in $2 : # End the grab for $2 at the colon \s+ # Skip any white space ( # Going to capture into $3 (?: # Start non-capturing parens (?! # Start negative lookahead \s* # Look for some white space \(rev # Followed by (rev ) # Close negative lookahead .) # Close non-capturing parens +) # So if whitespace followed by (rev, grab it $3 \s* # Up until some white space (.*) # Grab everything else after the white space into +$4 /x;

        I really just wanted to demonstrate to the OP how to powerfully use the /x modifier