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
3
votes
1 answer

lua pattern, exclusion doesn't work with end of string

Maybe exclusion is not the correct term, but I'm talking about using the following in lua's string.find() function : [^exclude] It doesn't seem to work if the character is followed by nothing, IE it's the last character in the string. More…
Jivex5k
  • 121
  • 14
3
votes
2 answers

Splitting string into substrings in Lua

I am trying to split a string to substrings using Lua. Using the pattern in the for loop below I would have expected 4 matches but I only get 2. print(words[1]) displays "###Lorem ipsum dolor sit amet, Gruß consetetur sadipscing elitr, sed diam…
Simone Luu
  • 107
  • 1
  • 10
3
votes
3 answers

Lua gsub second instance

I'm using local mystring = 'Thats a really nice house.' string.gsub(mystring,"% ", "/",1) to replace the first white space character with an slash. But how to replace only the second occurrence of the white space?
frgtv10
  • 5,300
  • 4
  • 30
  • 46
3
votes
0 answers

Lua Pattern Matching - force optional parameters to be filled before it moves on?

I need a pattern that will grab values for two or more parameters of varying length. I need to convert a scanf format %i to a lua pattern and it is proving very difficult. I don't need to worry about the type of storage that can be passed in with…
Birdbuster
  • 1,499
  • 1
  • 9
  • 13
3
votes
1 answer

A couple of pattern matching issues with pattern matching in Lua

I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specific and has no bearing on the Lua coding.) if not…
Josh
  • 3,225
  • 7
  • 30
  • 44
3
votes
1 answer

Print value from a lua table if pattern matches

Okay, so I just recently got into lua and find myself stuck with the following: I have the function peripheral.getNames() (which is a custom function) it will return a table with the structure key,value, whereas key is always a number and starts…
3
votes
2 answers

What does this pattern ^[%w-.]+$ mean in Lua?

Just came across this pattern, which I really don't understand: ^[%w-.]+$ And could you give me some examples to match this expression?
Badesign
  • 37
  • 1
  • 4
3
votes
1 answer

Trying to understand Lua simple codes

I'm having some trouble with Lua. The thing is: there are some Lua codes I know what they do but I didn't understood them, so if the professors ask me to explain them, I wouldn't be able to do it. Can you help me with this? I know this code…
cfrancklin
  • 315
  • 3
  • 16
3
votes
1 answer

string.match throwing error: attempt to index field '?' (a string value)

I am trying to match three pieces of data on a line of text in a text file and store them in table elements. Each line looks something like this: 0.277719 0.474610 This 0.474610 0.721241 is 0.721241 1.063209 test I have a local…
joed4no
  • 1,243
  • 13
  • 17
3
votes
2 answers

Pattern for characters that might be in the string

I understand that I should be using string.match() to do this but I am having trouble matching characters that "might" be in the string, for example: teststring = "right_RT_12" I can easily do something like: string.match(teststring ,…
iGwok
  • 323
  • 5
  • 18
3
votes
2 answers

Equivalent pattern to "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" in Lua 5.1

When answering this question, I wrote this code to iterate over the UTF-8 byte sequence in a string: local str = "KORYTNAČKA" for c in str:gmatch("[\0-\x7F\xC2-\xF4][\x80-\xBF]*") do print(c) end It works in Lua 5.2, but in Lua 5.1, it…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3
votes
1 answer

Any Lua pattern alternative to the regex (\\.|.)?

There's a common idiom for traversing a string whose characters may be escaped with a backslash by using the regex (\\.|.), like this: alert( "some\\astring".replace(/(\\.|.)/g, "[$1]") ) That's in JavaScript. This code changes the string…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
3
votes
1 answer

Use of ^ and gsub in Lua

function title case(theString) return (theString:gsub("^%a", string.upper):gsub("%s+%a", string.upper)) end I have the above mentioned code. I wanted to know the use of ^ operator in the above code. I know that ^ is used in sets to…
systemdebt
  • 4,589
  • 10
  • 55
  • 116
3
votes
1 answer

A calculator using Lua string matching

I've recently playing around with string manipulation to try to make a calculator that takes only one string and returns an answer. I know I could simply use loadstring to do this, but I am trying to learn more about string manipulation. This is…
user3314993
  • 287
  • 1
  • 4
  • 11
3
votes
1 answer

Why does string.find return the indexes of the entire pattern, not the first capture?

I was wondering about the behavior of this code: str = "abcd" print( str:find"a(bc)d" ) -- prints 1 4 bc print( str:find"(ab)cd" ) -- prints 1 4 ab Even though both of the two lines are looking for, and return, different strings,…
Wutaz
  • 362
  • 3
  • 19