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.

307 questions
63
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,411
  • 7
  • 33
  • 51
38
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
  • 383
  • 1
  • 3
  • 4
20
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,011
  • 2
  • 16
  • 21
13
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,292
  • 4
  • 27
  • 44
10
votes
3 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?
iRyanBell
  • 3,127
  • 6
  • 41
  • 69
10
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,132
  • 1
  • 14
  • 30
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
  • 17,193
  • 16
  • 68
  • 92
10
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
  • 101
  • 1
  • 1
  • 4
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,683
  • 1
  • 23
  • 43
9
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
  • 167
  • 1
  • 10
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
  • 225
  • 2
  • 7
9
votes
1 answer

Get file name from URL using Lua

How do I get the file name from a URL using Lua string manipulations. I have this url https://thisisarandomsite.com/some_dir/src/blah/blah/7fd34a0945b036685bbd6cc2583a5c30.jpg And I want to get the 7fd34a0945b036685bbd6cc2583a5c30.jpg, it can be a…
NaviRamyle
  • 3,827
  • 1
  • 28
  • 49
9
votes
2 answers

How to implement string.rfind in Lua

In Lua there's only string.find, but sometime string.rfind is needed. For example, to parse directory and file path like: fullpath = "c:/abc/def/test.lua" pos = string.rfind(fullpath,'/') dir = string.sub(fullpath,pos) How to write such…
mos
  • 225
  • 2
  • 7
9
votes
3 answers

What is the alternation operator in Lua patterns?

In regex, | is used for alternation. What is the corresponding character in Lua patterns?
Jason
  • 123
  • 1
  • 5
8
votes
1 answer

How to find a duplicate string with Pattern Matching?

I have a string similar to this: [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> |Hunit:Player-3693-07420299:DevnullYour [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265…
dev404
  • 1,049
  • 12
  • 29
1
2 3
20 21