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
1
vote
1 answer

Lua pattern doesn't work

I have trouble on my test script names.txt contents foo 1 test 0 data="names.txt" name="test" -- d=io.open(data,"r") s=d:read("*a") f=string.gsub(s,"%"..name,"%1 1") print(f) print"------" print(f:gsub("(%w+)%s*(%d)","%1"):format("%s")) output on…
1
vote
5 answers

What's a better way to write this regex, that make sure the target string contains at least one dot?

I need to use regex to filter a string, the string will contain at least one dot, and surrounded by a limited charset, So I used (Ignored all spaces): ^[a-z0-9:_-]+ \. [a-z0-9:_-]+$ The problem is that I need to use the exact same regex [a-z0-9:_-]+…
daisy
  • 22,498
  • 29
  • 129
  • 265
1
vote
2 answers

In Lua, how to find the end of an URL?

I try to parse a string with http URL in it, for example, the string is like str = "http://www.abc.com?id=123&key=456 and more text here" I want to know where the http url link ends, basically I use string.find(str, "......") what pattern can I…
Joe Huang
  • 6,296
  • 7
  • 48
  • 81
1
vote
1 answer

String manipulation in Lua with special characters

I would like to trim a string in Lua but I'm struggling I think because of the special characters in the string. E.g. str = "RG Ph 0%/15.00bpm" I would like to remove everything after and including the "/" so thatstr = "RG Ph 0%" I have found the…
1
vote
1 answer

Addon (WoW, Lua) causing critical error

The addon I'm making searchs for certain words within chats and then sends them to a specified chatframe it works but randomly it causes an error and crashes the game This application has encountered a critical error: ERROR #132 (0x85100084) Fatal…
1
vote
2 answers

Why does ("bar"):find("(foo)?bar") return nil?

Shouldn't ("bar"):find("(foo)?bar") return 1, 3? print(("bar"):find("(foo)*bar")) and print(("bar"):find("(foo)-bar")) won't work either.
Alexandre Kirszenberg
  • 35,938
  • 10
  • 88
  • 72
1
vote
2 answers

lua: pattern matching and extracting a phone number

I am having trouble crafting a function that has the following requirements in Lua: Takes a string phone_number and 2-digit country_code as input. phone_number has the form {1 || ""}{country_code}{10 or 11-digit mobile number} I need as output the…
Neil
  • 7,042
  • 9
  • 43
  • 78
0
votes
1 answer

How to get one, two, three or none occurrences with string.match in Lua?

Well given a string like this: local filename = "Help1_Index of Contents_Comments_EN.png", I can get the three occurrences ( are they even called occurrences? Anyway...) by means of this: local first, second, third =…
Rai
  • 314
  • 1
  • 2
  • 9
0
votes
1 answer

How can I tell if a string starts with an emoji or not in Lua 5.2?

I know trying to deal with emojis in a string can get complicated in some cases, but in case I only wanted to know if a string starts with an emoji (any) or not, is there an easy way? Sorry if it finally turned to be a very basic question, but the…
Rai
  • 314
  • 1
  • 2
  • 9
0
votes
0 answers

In Lua, how can I remove a word from a string which can be preceded for certain character or not?

Lets say I have a string like this: And I need to add and delete names at will. What would be the better way of doing it without the risk of leaving any leading ";" here and there? Since, depending on its position, the name could be preceded for a…
Rai
  • 314
  • 1
  • 2
  • 9
0
votes
2 answers

Trying to get some kind of key:value data from a string in Lua

I'm (again) stuck because patterns... so let's see if with a little of help... The case is I have e. g. a string returned by a function that contains the following: My…
Rai
  • 314
  • 1
  • 2
  • 9
0
votes
2 answers

Stop matching after first occurence (single line)

How could I just match the first anchor tag and not all of them until the last one? Basically all of this: "..." without the other ones? Would I need to sub the string before matching? Here's what I…
uhi_o
  • 23
  • 4
0
votes
2 answers

Regex with optional number and leading char ? (lua match)

I'm a beginner with regex and I'd like (for string.match() under lua) a regex that would recognize a positive or negative number prefixed by a special character (example : "!"). Example : "!1" -> "1" "!-2" -> "-2" "!+3" -> "+3" "!" -> "" I…
DaveInDev
  • 27
  • 8
0
votes
0 answers

Given this Lua string pattern for getting path/filename/extension, how could I get the extension WITH the dot?

The following function...