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

Lua pattern matching vs. regular expressions

I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in the lua documentation on lua.org: Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with…
aurora
  • 9,607
  • 7
  • 36
  • 54
57
votes
1 answer

Lua String replace

How would i do this? I got this: name = "^aH^ai" string.gsub(name, "^a", "") which should return "Hi", but it grabs the caret character as a pattern character What would be a work around for this? (must be done in gsub)
Frank
  • 573
  • 1
  • 4
  • 4
23
votes
3 answers

What is the neatest way to split out a Path Name into its components in Lua

I have a standard Windows Filename with Path. I need to split out the filename, extension and path from the string. I am currently simply reading the string backwards from the end looking for . to cut off the extension, and the first \ to get the…
Jane T
  • 2,081
  • 2
  • 17
  • 23
18
votes
3 answers

Logical 'or' in Lua patterns?

Is it possible to achieve in Lua? local noSlashEnding = string.gsub("slash\\ending\\string\\", "\\|/$", "") -- noSlashEnding should contain "slash\\ending\\string" local noSlashEnding2 = string.gsub("slash/ending/string/", "\\|/$", "") --…
Yuri Ghensev
  • 2,507
  • 4
  • 28
  • 45
13
votes
4 answers

Capitalize first letter of every word in Lua

I'm able to capitalize the first letter of my string using: str:gsub("^%l", string.upper) How can I modify this to capitalize the first letter of every word in the string?
user479947
13
votes
3 answers

Split a string using string.gmatch() in Lua

There are some discussions here, and utility functions, for splitting strings, but I need an ad-hoc one-liner for a very simple task. I have the following string: local s = "one;two;;four" And I want to split it on ";". I want, eventually, go get {…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
12
votes
2 answers

Lua Match Everything After Character in String

I am new to Lua, and hardly understand pattern matching. I am trying to figure out how to match everything in a string after a colon, and put that portion of the string into a variable. I haven't had much luck by looking around online, or maybe I am…
12
votes
3 answers

Finding '.' with string.find()

I'm trying to make a simple string manipulation: getting the a file's name, without the extension. Only, string.find() seem to have an issue with dots: s = 'crate.png' i, j = string.find(s, '.') print(i, j) --> 1 1 And only with dots: s =…
user2141781
  • 121
  • 1
  • 1
  • 4
10
votes
3 answers

Which characters are included in the Lua punctuation string pattern (%p)?

I haven't been able to find documentation of which characters compound the punctuation set "%p" in Lua.
user3325563
  • 187
  • 2
  • 12
10
votes
2 answers

Lua: Substitute list of characters in string

Is it possible to substitute characters according to a list in Lua, like tr in Perl? For example, I would like to substitute A to B and B to A (e.g. AABBCC becomes BBAACC). In Perl, the solution would be $str ~= tr/AB/BA/. Is there any native way of…
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
10
votes
1 answer

remove '$' characters from a string

I am trying to remove '$' signs from a string, but I am guessing it is some special char? I am extremely new to lua (just started coding in it today). From my understanding this should work and does for other chars string.gsub(line,'$','').
Richard
  • 15,152
  • 31
  • 85
  • 111
9
votes
1 answer

A regex I don't understand

I'm starring at these few (slightly modified) lines from luadoc that are obviously building a filename with a full path. But I simply don't get it what happens in line 5. The parameter filename could be something like "myfile.lua". function out_file…
h0b0
  • 1,802
  • 1
  • 25
  • 44
9
votes
1 answer

Amount of repetitions of symbols in Lua pattern setup

I'm looking for amount of repetitions of symbols in Lua pattern setup. I try to check amount of symbols in a string. As I read in manual, Even with character classes this is still very limiting, because we can only match strings with a fixed…
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
9
votes
2 answers

Optional Group Capture with Lua Pattern Matching

I am trying to parse chemical formulas in Lua using simple pattern matching. However, I do not know how to specify a capture group as being optional. Here is the pattern I have come up with: pattern = "(%u%l*)(%d*)" The first group captures the…
Moop
  • 3,414
  • 2
  • 23
  • 37
9
votes
1 answer

Is there a Lua string.find without pattern

I apply a function, but looks so bad. function find_without_pattern(s1,s2) for i =1,#s1-#s2+1 do local t = string.sub(s1,i,#s2+i-1) if t == s2 then return i,i+#s2-1 end end end
mos
  • 235
  • 3
  • 7
1
2 3
23 24