lua-users home
lua-l archive

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


Ow, my Lua supports '?' quantifiers on capture groups. Looks like core Lua doesn't.

Alternative (note: the patterns are anchored now at start and end, to match whole string before making any substitution):

link.target = link.target
  :gsub('^(.+)%.md#(.*)$', '%1.html#%2')
  :gsub('^(.+)%.md$', '%1.html')
 
 

Le mar. 26 janv. 2021 à 05:15, Peter Matulis <pmatulis@gmail.com> a écrit :
It looks like the '?' quantifier can only apply to a single character or class of characters, not a matching pattern like '#.+'

On Mon, 25 Jan 2021 at 14:45, Peter Matulis <pmatulis@gmail.com> wrote:


On Thu, 21 Jan 2021 at 11:53, Philippe Verdy <verdyp@gmail.com> wrote:
Note that Lua patterns do not support alternatives with '|', but they support  '?' for 0 or 1 occurrence (not just '*' and '+' for 0+ and 1+ occurences).
Note also that you want to match the dot for the extension literally, not arbitrary characters, so you must use '%.', not '.'. The '#' does not need to be escaped by a '%' in the search pattern (however it still works if you do).
Just create a capture group for the optional anchor (*including* the leading '#').

function Link (link)
  link.target = link.target:gsub('(.+)%.md(#.+)?', '%1.html%2')
  return link
end


This does not work for me. Neither of the alternatives are acted upon. How should I debug this?

/pm