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
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,967
  • 1
  • 31
  • 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
  • 235
  • 3
  • 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
3 answers

Modifying a character in a string in Lua

Is there any way to replace a character at position N in a string in Lua. This is what I've come up with so far: function replace_char(pos, str, r) return str:sub(pos, pos - 1) .. r .. str:sub(pos + 1, str:len()) end str = replace_char(2,…
dotminic
  • 1,135
  • 2
  • 14
  • 28
8
votes
3 answers

Split a string by \n or \r using string.gmatch()

A simple pattern should do the job but I can't come up with/find something that works. I am looking to have something like this: lines = string.gmatch(string, "^\r\n")
Nyan Octo Cat
  • 159
  • 2
  • 4
  • 8
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,088
  • 13
  • 34
8
votes
2 answers

How to capture a string between parentheses?

str = "fa, (captured)[asd] asf, 31" for word in str:gmatch("\(%a+\)") do print(word) end Hi! I want to capture a word between parentheses. My Code should print "captured" string. lua: /home/casey/Desktop/test.lua:3: invalid escape sequence…
Jin Su Lee
  • 83
  • 1
  • 4
8
votes
4 answers

Lua string.gsub with Multiple Patterns

I am working on renaming the Movie titles that has unwanted letters. The string.gsub can replace a string with "" nil value but I have around 200 string patterns that need to be replaces with "". Right now I have to string.gsub for every pattern. I…
Prakash.DTI
  • 913
  • 2
  • 9
  • 19
8
votes
4 answers

Lua gmatch odd characters (Slovak alphabet)

I am trying to extract the characters from a string of a word in Slovak. For example, the word for "TURTLE" is "KORYTNAČKA". However, it skips over the "Č" character when I try to extract it from the string: local str = "KORYTNAČKA" for c in…
Omid Ahourai
  • 1,409
  • 2
  • 14
  • 18
8
votes
6 answers

Iterate over lines including blank lines

Given a multiline string with some blank lines, how can I iterate over lines in Lua including the blank lines? local s = "foo\nbar\n\njim" for line in magiclines(s) do print( line=="" and "(blank)" or line) end --> foo --> bar --> (blank) -->…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
8
votes
2 answers

Lua pattern parentheses and 0 or 1 occurrence

I'm trying to match a string against a pattern, but there's one thing I haven't managed to figure out. In a regex I'd do this: Strings: en eng engl engli englis english Pattern: ^en(g(l(i(s(h?)?)?)?)?)?$ I want all strings to be a match. In Lua…
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
7
votes
2 answers

how to check if a word appears as a whole word in a string in Lua

not sure how to check if a word appears as a whole word in a string, not part of a word, case sensitive. for example: Play is in strings Info Playlist Play pause but not in the strings Info Playlist pause Info NowPlay pause
mile
  • 402
  • 1
  • 6
  • 15
7
votes
1 answer

How do I replace a $ in a Lua string?

How do you replace a dollar sign in Lua since it is a special character in pattern matching? I've tried this: string.gsub("$44,000.00", "$", "") > "$44,000.00" But all it does is add a blank at the end of the string. For…
Wryte
  • 885
  • 2
  • 10
  • 23
7
votes
1 answer

Lua pattern for guid

I am trying to implement a pattern in Lua but no success The pattern I need is like regex: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} which is to validate guid. I am not able to find proper way to find the implement regex in Lua…
7
votes
3 answers

Lua string manipulation pattern matching alternative "|"

Is there a way I can do a string pattern that will match "ab|cd" so it matches for either "ab" or "cd" in the input string. I know you use something like "[ab]" as a pattern and it will match for either "a" or "b", but that only works for one letter…
1
2
3
23 24