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

string.sub issue with non-English characters

I need to get the first char of a text variable. I achieve this with one of the following simple methods: string.sub(someText,1,1) or someText:sub(1,1) If I do the following, I expect to get 'ñ' as the first letter. However, the result of either…
C. Ulker
  • 43
  • 3
4
votes
1 answer

Creating arrays in Lua from list returned by gmatch

I am programming in Lua and so far I have this. S=tostring(M[i].AllSegmentsList) --it returns "MSH, PID" for i in string.gmatch(S, ",") do --I have ", " as delimiter t= {} --Now, I want the values returned by delimiter to be…
systemdebt
  • 4,589
  • 10
  • 55
  • 116
4
votes
2 answers

Is there an efficient and elegant way to truncate number like this in Lua?

What I want to do What I want to do is really simple. I want use Lua to check lines in a Plist file. Let's say if a line in Plist, is -1.00, I need to cut the .00 off to make it be -1. What I did I use a…
Tim
  • 2,121
  • 2
  • 20
  • 30
4
votes
2 answers

Optional capture of balanced brackets in Lua

Let's say I have lines of the form: int[4] height char c char[50] userName char[50+foo("bar")] userSchool As you see, the bracketed expression is optional. Can I parse these strings using Lua's string.match() ? The following pattern works for…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
4
votes
1 answer

Lua string.match pattern for MSN weatherservice

I use the MSN weatherservice. Now I have the following problem with string.match. All variables are filled except sWindRichtung. It equals nil. sHumidity, rest = string.match(rest,"humidity=\"([^\"]+)\"(.*)"); sWind, rest =…
Mark Munsterman
  • 169
  • 1
  • 3
  • 11
4
votes
7 answers

How to validate this string when we don't have the `|` operator in Lua?

I have strings of the form: cake!apple! apple! cake!juice!apple!cake! juice!cake! In other words, these strings are composed of the three sub-strings "cake!", "apple!" and "juice!". I need to validate these strings. The way to do this with a…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
4
votes
1 answer

Why is lua's string pattern matching doing this?

I have an external application which monitors CPU and GPU temperatures... I am using Lua with the alien extension to grab these values (via GetWindowText) and to do some pattern matching on these values, effectively extracting the temperature digits…
RectangleEquals
  • 1,825
  • 2
  • 25
  • 44
4
votes
2 answers

what are the use of parentheses in this Lua pattern?

base string is: IP: 192.168.0.1 Passing that string to string.gmatch function(besides below patterns) will return the following results: pattern: IP: (%d+.%d+.%d+.%d+) -->192.168.0.1 pattern: IP: %d+.%d+.%d+.%d+ -->IP: 192.168.0.1 My question is…
wiki
  • 1,877
  • 2
  • 31
  • 47
4
votes
1 answer

putting '%' in string.gsub() in Lua

i have this code local strs = "my dog" print (string.gsub( strs , " ","%20")) i just wanted the output will be like this my%20dog but i got this error Runtime error ... invalid capture index stack traceback: [C]: ? [C]: in…
gadss
  • 21,687
  • 41
  • 104
  • 154
3
votes
2 answers

Problem to convert a String into a Table with pattern matching

Given is the string: local a = "#5*$4a+02/+2-%110" Now i want to split the following string into a table like b[1]="5" b[2]="*" b[3]="$4a" b[4]="+" ... b[10]="%110" My solution was local b = {} for part in…
a.c.m.
  • 31
  • 2
3
votes
1 answer

Lua Pattern matching only returning first match

I can't figure out how to get Lua to return ALL matches for a particular pattern match. I have the following regex which works and is so basic: .*\n This just splits a long string per line. The equivelent of this in Lua is: .-\n If you run the…
Mucker
  • 257
  • 3
  • 12
3
votes
1 answer

Lua gmatch store captured groups as an array

I'm rather new to Lua. For each match of gmatch, I would like to put the capture group results into an array. The idea is so I get all the capture groups for each match, as an array, so I can do operations on this array, e.g. convert each capture…
simonzack
  • 19,729
  • 13
  • 73
  • 118
3
votes
2 answers

Remove spaces from a string but not new lines in lua

I used string.gsub(str, "%s+") to remove spaces from a string but not remove new lines, example: str = "string with\nnew line" string.gsub(str, "%s+") print(str) and I'm expecting the output to be like: stringwith newline what pattern should I use…
ahmed
  • 88
  • 1
  • 9
3
votes
2 answers

Lua patterns - How to remove unwanted string within a string

I am receiving bunch of rows like below 2011/02 ARRTC AAUUMCO ZZITNWMOBILE COMMUNICATIONS CENTER ARRTC-AAUUM-TBT-2011-02 0.00 AAUUM_ARRTC_0211_TBT
Shax
  • 4,207
  • 10
  • 46
  • 62
3
votes
2 answers

Include empty matches when using string.gmatch to split a string in lua 5.1

I have a comma seperated input string that needs to support empty entries. So a string like a,b,c,,d should result in a table with 5 entries, where the 4'th is an empty value. A simplified example str="a,b,c,,d" count=0 for v in string.gmatch(str,…
Daniel
  • 10,641
  • 12
  • 47
  • 85