lua-users home
lua-l archive

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

On 16/11/13 21:22, Liam Devine wrote:

On 16/11/13 20:59, Michael Savage wrote:

Hi lua-l,

( "[a] [b] c" ):match( "%[(.-)%] c" )
-> "a] [b"

From or

a single character class followed by '-', which also matches 0 or more
repetitions of characters in the class. Unlike '*', these repetition
items will always match the shortest possible sequence;

The shortest possible sequence for the above is "b". Is this a bug?


It is not, as it returns the first match [1]. If you wanted to capture
the last sequence in square brackets you could prefix it with a greedy
( "[a] [b] c" ):match( ".*%[(.-)%] c" )


I say could because it all depends on if you know your data format, the more you know the more specific you can be.
- Single lower case character
- Multiple lower case characters
- Numbers and lower case characters
- Upper and lower case characters

> print( ( "[a] [b] c" ):match( "(%l)%] c$" ) )
> print( ( "[a] [bddd] c" ):match( "(%l-)%] c$" ) )
> print( ( "[a] [b1d2c] c" ):match( "([%l%d]-)%] c$" ) )
> print( ( "[a] [KJhhgGGj] c" ):match( "(%a-)%] c$" ) )

As you can see I also like to anchor the match when possible.