in reply to Re: splitting lspci output
in thread splitting lspci output

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 ?

Replies are listed 'Best First'.
Re^3: splitting lspci output
by GrandFather (Saint) on May 18, 2006 at 04:17 UTC

    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