lua-users home
lua-l archive

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


I have a regex problem in 5.2.4 either under Cygwin or Ubuntu 16.04.

I want to match two character strings like Aa, AA, A2 where the first
letter is a capital and the second could also be a digit using
string.gmatch to generate an iterator.

If I check with string.match this works:

print(string.match("blah blah Aa boo ","%u%w"))
print(string.match("blah blah AA boo ","%u%w"))
print(string.match("blah blah A3 boo ","%u%w"))

But if I use gmatch as in a simple function:

local str=" A1 blah AA boo Ax "

local function getCase(A,str)
  local nextCase = string.gmatch(str,"%u%w")
  local N=nextCase()
  while N ~=  nil do
    if type(Q[N]) == 'table' then A[N] = #Q[N] end
  return A

The function takes a table and checks whether Q[A3] for example exists and
is a table, if so, returning the length of the table in A.

I have added print instructions but the iterator does not find the cases
where there is a number involved.

I have tried all sorts of regexes:  %u%w, %u[%a%d], %u[a-zA-Z0-9] all to
no avail.  I cannot detect the cases where there is a digit.

Any clues what I am doing wrong, when string.match will detect the string
correctly.  Is there a problem with string.gmatch?

I could of course use string.find (which also works correctly) starting
with the end (or 1) to get round the issue but that is rather inelegant :(

Best wishes


John Logsdon
Quantex Research Ltd
+44 161 445 4951/+44 7717758675