It is currently April 25th, 2024, 9:17 am

[HELP] string.match and regular expressions

Discuss the use of Lua in Script measures.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

[HELP] string.match and regular expressions

Post by smurfier »

I'm really bad with regular expressions so I need to ask for help.

I'm retrieving lines from a text file and some of the lines include the following values.

Code: Select all

FirstSun, SecondSun, ThirdSun, FourthSun, LastSun
FirstMon, SecondMon, ThirdMon, FourthMon, LastMon
FirstTue, SecondTue, ThirdTue, FourthTue, LastTue
FirstWed, SecondWed, ThirdWed, FourthWed, LastWed
FirstThu, SecondThu, ThirdThu, FourthThu, LastThu
FirstFri, SecondFri, ThirdFri, FourthFri, LastFri
FirstSat, SecondSat, ThirdSat, FourthSat, LastSat
What I need to do is take lets say FirstSun and separate the First from the Sun and save them both to separate variables.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: string.match and regular expressions

Post by donutmtoazt »

I think you need something like this:

Code: Select all

var1, var2 = string.match("FirstSun", "(.*)(...)")
This should work on all of your strings you listed, and it assigns the strings to the variables respectively. And basically, (.*) captures all of the letters before (...), which captures the last three letters.
This is my keyboard. There are many like it, but this one is mine.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: string.match and regular expressions

Post by donutmtoazt »

Unless your full string is this:

Code: Select all

FirstSun, SecondSun, ThirdSun, FourthSun, LastSun
in which case, the code isn't too different:

Code: Select all

varfirst, varsecond, varthird, varfourth, varlast, varsun = string.match(stringvariable, "(.*)..., (.*)..., (.*)..., (.*)..., (.*)(...)")
That will give you the strings "First", "Second", "Third", "Fourth", "Last", and "Sun" respectively. I figured you wouldn't need five "Sun"'s.
This is my keyboard. There are many like it, but this one is mine.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: string.match and regular expressions

Post by Yggdrasil »

I suggest this:

Code: Select all

local var1,var2,var3,var4,var5 = line:match("^(%a),%s(%a),%s(%a),%s(%a),%s(%a)$")
%a represents the letters, so it's the same as [a-zA-Z]. As the lines have the same format, it's the best way. ^ means that Lua must start the searching from the begin of the line, and $ marks the end of the line. %s is one space character, the comma represents itself. You should always use the most punctual pattern you can write, it makes Lua faster.
Now var1 is FirstSun in the first line. If you want to separate the two word, use this:

Code: Select all

local num,day = var1:match("^(%u%l+)(%u%l%l)$")
%u represents the uppercase letters (thus, [A-Z]), and %l is the lowercase letters ([a-z]).
%u%l+%u%l%l means: one uppercase, one or more lowercase letter, until it finds an uppercase and two lowercase letters.
Image
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: string.match and regular expressions

Post by donutmtoazt »

Good point. I just recently started learning Lua, so my code is usually a bit rough anyways. I'll remember that.

Thanks!
This is my keyboard. There are many like it, but this one is mine.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: string.match and regular expressions

Post by smurfier »

donutmtoazt wrote:I think you need something like this:

Code: Select all

var1, var2 = string.match("FirstSun", "(.*)(...)")
This should work on all of your strings you listed, and it assigns the strings to the variables respectively. And basically, (.*) captures all of the letters before (...), which captures the last three letters.
This is exactly what I need. Thank you very much. One day I may actually learn how to use regular expressions so I don't have to ask for help every time I want to use one.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: string.match and regular expressions

Post by donutmtoazt »

Well, if we're getting technical, Lua doesn't use Regular Expressions. It uses "Patterns". ;)

I dunno, I read that somewhere and I thought it was hilarious that Lua has to specify that difference, though the two are so similar. Eh, bad jokes for the win. It would've worked too if there was a sarcasm font...
This is my keyboard. There are many like it, but this one is mine.