[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [string.gmatch] Getting rid of parentheses?
- From: Rob Kendrick <rjek@...>
- Date: Mon, 25 Jul 2011 10:00:50 +0100
On Mon, Jul 25, 2011 at 10:53:45AM +0200, Gilles Ganault wrote:
> Hello
>
> Google didn't help to figure out why Lua returns found tokens between
> parentheses:
>
> =========
> page = '<span>item1</span><br>(item2)<br>(item3)<br><i>'
>
> for a,b in string.gmatch(page,
> '<span>.-</span><br>(.-)<br>(.-)<br><i>') do
> stuff = a .. b .. "\n"
> print(stuff)
> end
> =========
>
> Here's what the console says:
> =========
> (item2)(item3)
> =========
>
> What's the right way to extract tokens from a string without having
> them displayed with parentheses?
Don't include the parentheses in the capture. The trouble you have here
is that you're matching everything between the <br> tags and capturing
it, because parentheses describe what to capture. You can escape with
%:
> page = '<span>item1</span><br>(item2)<br>(item3)<br><i>'
> =page:match "<span>.-</span><br>(.-)<br>(.-)<br><i>"
(item2) (item3)
> =page:match "<span>.-</span><br>%((.-)%)<br>%((.-)%)<br><i>"
item2 item3
>
B.