lua-users home
lua-l archive

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


Hi Theodor!

If I understand you correctly, main goal is to eliminate second call of
string.find(). Although you didn't provide a sample of your strings I
hope my test sample will be helpful. 

Main idea is to use for first capture "(.*)" instead of "(.)". And, in
case of first type of string you'll receive a symbol and in case of
second type it will be an empty string. Empty string may be easity
converted to nil, if it required.

Maybe there's a better solution, which will give you exactly you want
(nil for the first capture), but at a first sight I suppose my version
completely fits your needs.

WBR, Denis

-----------------------
m1 = "(.)(.)(.)"
m2 = "(.*)(.)(.)"
str1 = "ABC"
str2 = "BC"

_, _, cap1, cap2, cap3 = string.find( str1, m1 )
print( cap1, cap2, cap3 )
_, _, cap1, cap2, cap3 = string.find( str2, m1 )
if cap1 == "" then cap1 = nil end
print( cap1, cap2, cap3 )
---------------------


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Theodor-Iulian
Ciobanu
Sent: Friday, October 27, 2006 10:11 AM
To: Lua list
Subject: Need help building a regexp pattern


Hello Lua,

  Most of the questions I asked here are related to parsing of logs, and
this one makes no exception. I'm blaming it on me still not having a
good grasp of the language, but I do love its speed and versatility.
('Cause I don't think I ever mentioned how much I *enjoy* working with
lua - great job, everybody!)

  To get back on topic, I have a log with lines that would match either
the pattern "(.)%s*(.-)%s*(.+)" or just "(.-)%s*(.+)". What I'd like to
be able to do is build a single pattern with three captures, the first
one being nil for the second type of strings. Is it possible? (Currently
I'm doing a string.match with the first pattern and if that fails then I
do a second one with the latter pattern - and this is the optimization
I'd like to do, have only one string.match())

-- 
Theodor-Iulian Ciobanu
jr Virus Researcher

e-mail: tciobanu@bitdefender.com

-----------------------------------------
Secure your every bit
-----------------------------------------


--
This message was scanned for viruses by BitDefender for Linux Mail
Servers. For more information please visit http://www.bitdefender.com/