lua-users home
lua-l archive

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


Right, I'm learning here but now I want to grok another pattern.

  Local Socket:   2001:db8:4545:2:0:9ff:ac11:4519..750

I want discard the label and extract the ipaddr and port. In REXX I would code

p = "Local Socket:   2001:db8:4545:2:0:9ff:ac11:4519..750"
parse var p . ":" ipaddr ".." port
say ipaddr port

where the . discards anything up to the pattern template.

When I master Lua I will write a library to simulate the REXX parse instruction. Steve Donovan has a great library that I will use as inspiration to do that.

I've fallen in love with Lua but IMHO REXX is much simpler for scraping report output.


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.

Philipp