Questions tagged [lua-patterns]

Lua's native string pattern matching facility. Note that Lua patterns are not equivalent to a regex.

Lua's built-in string patterns employ syntax similar to POSIX- or Perl-style regexes, but there are many basic differences. This tag should be used instead of the regex tag, to forestall irrelevant answers from responders who are familiar with regexes but not with Lua.

Using this tag instead of the regex also tag makes it clear that you're not using regexes provided via an imported library.

356 questions
4
votes
1 answer

Match if something is not preceded by something else

I'm trying to parse a string and extract some numbers from it. Basically, any 2-3 digits should be matched, except the ones that have "TEST" before them. Here are some examples: TEST2XX_R_00.01.211_TEST => 00, 01, 211 TEST850_F_11.22.333_TEST => 11,…
PoVa
  • 995
  • 9
  • 24
4
votes
2 answers

Lua Patterns - World of Warcraft Vanilla

I'm trying to get some data from the chat of the game but I can't figure out the pattern. It's for an AddOn for a World of Warcraft Vanilla (private server). gsub…
user7393973
  • 2,270
  • 1
  • 20
  • 58
4
votes
2 answers

Unexpected lua pattern matching result

For the following code: local function getParentPath(_path) pattern = "(.+)([/\\][/\\])(.+)" i,j,k = string.match(path,pattern) return i,j,k end print(getParentPath(path)) For path = "C://data//file.text", I get: C://data //…
tanzil
  • 1,461
  • 2
  • 12
  • 17
4
votes
3 answers

Pattern match a string in Lua

I have the following string to split into a table using Lua: (the data is aligned with each other. I did not find out how to write format it like that on this site) IP: 192.168.128.12 MAC: AF:3G:9F:c9:32:2E Expires: Fri Aug…
4
votes
3 answers

Trying to check if a string contains a given word

function msgcontains(msg, what) msg = msg:lower() -- Should be replaced by a more complete parser if type(what) == "string" and string.find(what, "|", 1, true) ~= nil then what = what:explode("|") end -- Check…
Raúl Sanpedro
  • 266
  • 4
  • 15
4
votes
2 answers

Lua pattern matching for email address

I having the following code: if not (email:match("[A-Za-z0-9%.]+@[%a%d]+%.[%a%d]+")) then print(false) end It doesn't currently catch "test@yahoo,ca" or "test@test1.test2,com" as an error. I thought by limiting the input to %a -…
dot
  • 14,928
  • 41
  • 110
  • 218
4
votes
3 answers

How to match a floating point number when reading a string

How can I match floating point numbers like 1.234 or that use the "E notation" like 1.23e04 when dealing with strings? As an example, let's say that I would like to read numbers from a data file like the following: 0.0 1.295e-03 0.1 1.276e-03 0.2…
Pier Paolo
  • 878
  • 1
  • 14
  • 23
4
votes
2 answers

Lua string.find can't find the last word in a line

It's a case in the book programming in Lua. The code is followed, my question is why it can't get the last word of the line? function allwords() local line=io.read() local pos=1 return function () while line do local…
hkk
  • 45
  • 5
4
votes
1 answer

Have Lua pathological patterns with exponential running time?

Its known that regular expressions implemented in a recursive fashion (instead of a NFA/DFA) can need in some cases exponential running time. Lua patterns are implemented via a recursive matcher (they allow backtracking), but they are less powerful…
user3368561
  • 779
  • 6
  • 18
4
votes
1 answer

Lua pattern to stop when end of line

I need to get help for a pattern in Lua stopping to read after a line break. My code: function getusers(file) local list, close = {} local user, value = string.match(file,"(UserName=)(.*)") print(value) f:close() end f =…
Pekinese
  • 177
  • 4
  • 18
4
votes
1 answer

Lua - Find "tags" in string?

I'm planning on using Lua patterns, unless there's a better way to do this. I want to be able to parse a string, and look for "tags". For example, I'd like to find the '[color=???][/color]' part of a string, not care what comes after the equals, not…
Eamonn
  • 658
  • 1
  • 7
  • 22
4
votes
1 answer

Check for valid domain

I want to add to this function Domain checking: GetType = function(ip) local Split = function(s,sep) local t={};i=1 for str in string.gmatch(s,"([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end local R =…
Col_Blimp
  • 779
  • 2
  • 8
  • 26
4
votes
2 answers

Lua pattern similar to regex positive lookahead?

I have a string which can contain any number of the delimiter §\n. I would like to remove all delimiters from a string, except the last occurrence which should be left as-is. The last delimiter can be in three states: \n, §\n or §§\n. There will…
user3746280
4
votes
2 answers

Store Lua string.match output to array

Normally I use two variables to store the output of something like this: a = {'alarm boy car dentist','alarm car dentist elephant','alarm dentist elephant fabulous','alarm elephant fabulous goat'} k, v = string.match(a[1], 'alarm dentist (%w+)…
user3325563
  • 187
  • 2
  • 12
4
votes
1 answer

finding a url in a string lua pattern

Using Lua Pattern Matching I would like to be able to parse a string and find the following URLs http://www.test.com/ www.test.com/ test.com/ test-test.test.com/ The slashes can be optional but if included it has to be able to find nested folders…
TULOA
  • 147
  • 1
  • 10