[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Regex question.

Regex question.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Blainer   (191 posts)  [Biography] bio
Date Mon 18 May 2009 04:50 PM (UTC)
Message
When I run this code:

t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end
for i,v in pairs(t) do Note(t[i]) end

I get output:
Lua
world

I am trying to learn regex so I can run this:

for v in io.lines([[speeds.txt]]) do
for k,w in string.gmatch(v,"pattern") do
-put each section into array
end
end

speeds.txt (I added the == trying to make some thing to match):

==1==Armory==Gueldar's Armory==2s3wn==
==2==Bakery==The Aylorian Eatery==2s3en==
==3==Bank==The Aylorian Bank of Ivar==11s==

I've been trying to regex for awhile now and noticed the output from the example above (taken from www) didn't seem to do what it was intended to do. So I figured I was missing something or using different syntax for patterns than MUSH.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Mon 18 May 2009 06:19 PM (UTC)
Message
What do you mean by not doing what it was supposed to do? Are you showing us your actual code? For instance you have your pattern as just "pattern" which won't do anything interesting.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Blainer   (191 posts)  [Biography] bio
Date Reply #2 on Mon 18 May 2009 07:12 PM (UTC)
Message
The first bit of code doesn't seem to do what the programmer intended this made me think there was a difference with the way MUSH was using regex as compared with info on the web, it only list 2 of the 4 words. It was an example I was using to learn regex.

Example from web:
====
t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end
for i,v in pairs(t) do Note(t[i]) end

I get output:
Lua
world
====

The second bit of code is my attempt to put a file into a table. I'm having troubles with the regex. I can't get it to match and noticed the code above did something that seemed wrong to me as I was trying to learn so I added it to post. "Pattern" would be a regex yes. I can't seem to get it to match any number of digits and any number of letters with spaces. I thought "=(%w*)=" would work but it doesn't.

This is the input file:
==1==Armory==Gueldar's Armory==2s3wn==
==2==Bakery==The Aylorian Eatery==2s3en==
==3==Bank==The Aylorian Bank of Ivar==11s==

I added the == to give my something to match.

This is what I have so far:
for v in io.lines([[speeds.txt]]) do
for k,w in string.gmatch(v,"pattern") do
-put each section into array
end
end

I am not even sure this is the best way to go about this but it seemed easier than string.find.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 18 May 2009 11:29 PM (UTC)
Message
Yes, but the word "pattern" won't match, as the word "pattern" doesn't appear in the string. You need to put in something that is likely to work, or we can't help you. For example:


for v in io.lines("speeds.txt") do
  for k, w in string.gmatch(v,"(%w+)=(%w+)") do
  --put each section into array
  end
end	


Even then this won't work on your test data, as you have == between each word, and the regexp has a single = sign. I am not sure you are understanding what a pattern is. You don't use the word "pattern", you have to make one that is relevant to what you are trying to match.

This page shows what you can use in a pattern:

http://www.gammon.com.au/scripts/doc.php?lua=string.find

The regexp patterns are the same for string.find as string.gmatch, they just work a bit differently in how they return the matching data.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Blainer   (191 posts)  [Biography] bio
Date Reply #4 on Tue 19 May 2009 03:03 AM (UTC)
Message
Sorry guys I've made these posts as clear as mud. I am asking two questions.

Question 1.
The code I am trying to make work is:

for v in io.lines([[c:\speeds.txt]]) do
for k,w in string.gmatch(v,"=(%w*)=") do
Note(w)
end
end

speeds.txt looks like this:
==1==Armory==Gueldar's Armory==2s3wn==
==2==Bakery==The Aylorian Eatery==2s3en==
==3==Bank==The Aylorian Bank of Ivar==11s==

The regex above matches the whole line and I can't figure out how to get it to match one part of a line at a time. I need to break speeds.txt into four parts per line, Number, Name, Disc, Speedwalk. I can't get my head around this. "=" is an alphanumeric so it matches the whole line, is there a better character I should use to mark the sections of speeds.txt. Or can a regex match each part of the line with no character marking the beginning and end of each section?

I have tried [] ranges to avoid matching the whole line and even tried using the exact words "==(1)==(Armory)==(Gueldar's Armory)==(2s3wn)==" to test and still can't match each section. As I understand gmatch should should match 4 times on the first line using "==(1)==(Armory)==(Gueldar's Armory)==(2s3wn)==" and iterate 4 times returning what is between the brackets. How ever when returns "Armory" twice when I try this.



Question 2.
If I run:

t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end
for i,v in pairs(t) do Note(t[i]) end

And I get output:
Lua
world

Is this code working correctly. As I understand gmatch with regex it should give the out put:
from
world
to
Lua

Sorry for all the confusion and thanks for the help.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Tue 19 May 2009 04:06 AM (UTC)
Message
Question 1.

The string.gmatch iterator returns the next match from the line. This is a single value (as you had a single capture), so for it to work you need something like this:


for v in io.lines("speeds.txt") do
  for w in string.gmatch(v,"=(%w+)=") do
    Note(w)
  end
end


Output

1
Armory
2s3wn
2
Bakery
2s3en
3
Bank
11s





Question 2.

You are capturing the information OK, but your debugging display is wrong - it only shows the value, not the key. You could fix that up, but I used tprint to show what was happening:



t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end

require "tprint"

tprint (t)

Output

"to"="Lua"
"from"="world"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Blainer   (191 posts)  [Biography] bio
Date Reply #6 on Tue 19 May 2009 05:16 AM (UTC)
Message
Ah great thanks Nick. I suspected my regex was correct.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,842 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]