lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 25/09/2013 1:06 PM, Philipp Janda wrote:
Hi!

Am 25.09.2013 06:50 schröbte David Crayford:
Thanks. This is how dumbstruck I am WRT pattern matching. I want to
parse the following piece of netstat output

SKRBKDC  00000099 UDP
   Local Socket:   172.17.69.30..464
   Foreign Socket: *..*

The top line is the user, connection id and state. All I want to do is
capture three whitespace seperated words.

In REXX I would do this:

parse var line userid connid state

What is the most succinct way of doing something similar in Lua?

print( line:match( "(%w+)%s+(%w+)%s+(%w+)" ) )

%w matches a single "word"-character (letters, numbers, and _).
%w+ means: at least one of those, and as much as possible.
(%w+) means: capture the match (copy the matching substring to the return values of `line:match` (if the whole match succeeds)). %s matches a single whitespace character (not just space, but tab, and newline, etc. as well). %s+ means: match at least one whitespace, but as much as possible at that position.


Thanks Philipp! Great explaination.

Philipp