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
7
votes
4 answers

Escape string for Lua's string.format

I have a string I need to add a variable to so I use the string.format method, but the string also contains the symbol "%20" (not sure what it represents, probably a white space or something). Anyway since the string contains multiple "%" and I only…
user1593846
  • 734
  • 1
  • 15
  • 34
6
votes
1 answer

Dealing with speech marks with string.match

I have an xml page that I have a monitoring system scanning, here is the source data: ` And here is the code I have so far: local…
greenage
  • 399
  • 3
  • 13
6
votes
3 answers

Lua: split string into words unless quoted

So I have the following code to split a string between whitespaces: text = "I am 'the text'" for string in text:gmatch("%S+") do print(string) end The result: I am 'the text' But I need to do this: I am the text --[[yep, without the…
m45t3r
  • 477
  • 4
  • 13
6
votes
1 answer

Lua string.gsub text between pattern

How would I extract in Lua a text between a pattern. For example s="this is a test string. something more" I would need only the date/time as result: 2014-05-03 23:12:08 print(string.gsub(s, "%")) doesn't work I…
christian Muller
  • 5,016
  • 6
  • 34
  • 44
5
votes
4 answers

What is the proper Lua pattern for quoted text?

I've been playing with this for an hour or tow and have found myself at a road block with the Lua pattern matching utilities. I am attempting to match all quoted text in a string and replace it if needed. The pattern I have come up with so far is:…
Wolftousen
  • 147
  • 2
  • 8
5
votes
3 answers

Pattern not matching *(%(*.%))

I'm trying to learn how patterns (implemented in string.gmatch, etc.) do work in Lua 5.3, from the reference manual. (Thanks @greatwolf for correcting my interpretation about the pattern item using *.) What I'm trying to do is to match '(%(.*%))*'…
user5066707
5
votes
1 answer

gsub in Lua. Unable to replace pattern

I want to replace all phrases $br$ in the string for the character '\n'. I write the following code: str = string.gsub("String 1 $br$ String 2", "$br$", "\n"). But this does not work and displays the string String 1 $br$ String 2. What am I doing…
Victor
  • 333
  • 2
  • 10
5
votes
3 answers

In Lua, can I easily select the Nth result without custom functions?

Suppose I am inserting a string into a table as follows: table.insert(tbl, mystring) and that mystring is generated by replacing all occurrences of "a" with "b" in input: mystring = string.gsub(input, "a", "b") The obvious way to combine the two…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
5
votes
3 answers

how to extract the domain from a URL

I need to extract domain (four.five) from URL (one.two.three.four.five) in a Lua string variable. I can't seem to find a function to do this in Lua. EDIT: By the time the URL gets to me, the http stuff has already been stripped off. So, some…
Xi Vix
  • 1,381
  • 6
  • 24
  • 43
5
votes
1 answer

Lua string.match to extract some values of a HTML

I'm using Lua string.match to extract some values of a HTML but I'm having some problems with some attributes. To extract a phone number like this: 0000-0000, I'm using the mask: local value = string.match(STRING, "%d%d%d%d-%d%d%d%d") But Lua is…
briba
  • 2,857
  • 2
  • 31
  • 59
5
votes
3 answers

String Manipulation in Lua: Make the odd char uppercase

I'm trying to do a library in Lua with some function that manipulate strings. I want to do a function that changes the letter case to upper only on odd characters of the word. This is an example: Input: This LIBRARY should work with any…
Luca93
  • 91
  • 6
5
votes
1 answer

Lua: pattern matching multi-characters with the ? repetition operator

According to the docs, all Lua repetition operators only work on single characters, so you can match string.match('123', '1?(%d+)') -- returns 23 but cannot match multi-character strings: string.match('123', '(12)?(%d+)') -- want this to return…
Neil
  • 7,042
  • 9
  • 43
  • 78
4
votes
2 answers

What is the RegEx equivalent of ".-" in Lua's pattern matching?

I'm porting some Lua code to JS and I haven't worked with Lua so far. There's the Lua pattern "^([^aeiouàèéêíòóôúïü]*)(.-)$" and I found the following explanation for the hyphen here: - Match the previous character (or class) zero or more times, as…
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
4
votes
1 answer

Lua: return content of "{foo}{bar}"

for a string like "{foo}{bar}" is there an easy str = "{foo}{bar}" first, second = str:gmatch(...)... should give first="foo" and second="bar" The problem is that foo itself can have some more parentheses, eg: str = "{foo {baz}{bar}" so that first…
Red-Cloud
  • 438
  • 6
  • 13
4
votes
1 answer

What's the fastest way to get a random pattern match from a very long string in lua?

I have a string with over 2 million characters, and I feel like my current way of finding a random match from a pattern isn't fast as it could be. local function getRandomMatch(string, pattern) local occurenceCount = select(2,…
Yuzu
  • 51
  • 6
1 2
3
23 24