1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 29 Jun 2013 08:14 AM |
What I want to do is use a string pattern that will divide the string into pieces, but returns the strings it's being divided by as their own. How would I do this?
Example:
x = "Hi, I'm Paul! " for s in x:gmatch("(.-)[%s,]")do print(s) end > Hi > , > > I'm > > Paul!
What string pattern would I use to get that result? |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:25 AM |
Whatever ability it is that you think have is not demonstrated to any extent and is not relevant to any topic or discussion that takes place in this forum.
- A ROBLOX Vet. |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 29 Jun 2013 08:26 AM |
| I've seen people with more advanced code that Scripting Helpers can't handle post here before. And this is the only thing in almost a year that I've needed help with lol. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:26 AM |
Wot.... If Scripting Helpers can't even solve this anymore...
I'm going to need to head on over to SH, because they are blowing right now apparently. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:28 AM |
x = "Hi, I'm Paul! " t = {} for s in x:gmatch("(.-)[%s,]")do table.insert(t,s) end return t
|
|
|
| Report Abuse |
|
|
Cupkle
|
  |
| Joined: 18 Jun 2012 |
| Total Posts: 180 |
|
|
| 29 Jun 2013 08:30 AM |
local pattern = "%S+" --Any sequence of non-whitespace characters (uppercase reverses the effect) local x = "Hi, I'm Paul! " for s in x:gmatch(pattern)do print(s) end
That returns: > Hi, > I'm > Paul!
Is that what you mean? |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:32 AM |
"but returns the strings it's being divided by as their own. How would I do this?"
I'm pretty sure he's looking to return a table of the strings separated by the gmatch. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:33 AM |
Here's the closest I can come to what you're wanting using only a string pattern:
x = "Hi, I'm Paul! " for s in x:gmatch("[a-zA-Z'!]*")do print(s) end
> Hi > > > I'm > > Paul!
Notice the comma is not included in line 2 of the output, but is still counted as being separated from the trailing space. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 08:34 AM |
| Oh, and do note the pattern is specific to the provided example. |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 29 Jun 2013 08:54 AM |
So is there no way to get the result of
Hi ,
I'm
Paul!
? |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 09:03 AM |
| Not without a second pass. |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 29 Jun 2013 10:16 AM |
| How would I make the second pass then? :p |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 29 Jun 2013 10:32 AM |
local str = "Hi, I'm Paul!";
local function split(s) local tbl = { }; for m, sp in (s.." "):gmatch("(.-)(%s)") do if (m:match("^%a+%p$")) then local m2, m3 = m:match("^(%a+)(%p)$"); tbl[#tbl + 1] = m2; tbl[#tbl + 1] = m3; else tbl[#tbl + 1] = m; end tbl[#tbl + 1] = sp; end return tbl; end
for _, s in pairs(split(str)) do print(s); end |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
| |
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 10:51 AM |
| For what you were asking for, SH never would have gotten it. This is more of a challenge than a help request. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2013 11:24 AM |
You've got a little problem if you want to use quotes: x = "Hi, I'm Paul! \"quote test\" " for s in x:gmatch("(.-)[%s,]")do print(s) end
Outputs:
Hi
I'm Paul! "quote test" |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 11:27 AM |
| I solved the problem he gave. A different scenario would not be the same problem. |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 29 Jun 2013 11:27 AM |
| @Julien: That can't be solved with Lua's patterns. For that you'd need to use regex, or write a lexer. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 11:28 AM |
| @Dr01d OP hasn't specified what he wants done when there are quotes in the string so there is no determining what "can't be done" without knowing what it is. |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 29 Jun 2013 11:31 AM |
| @Waffle: That's not what I'm talking about. I meant that Lua doesn't allow you to do conditonal patterns like full regex does. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 11:41 AM |
| And in this case, you didn't need to. As I already pointed out, `[,%a][%a']-['%a]*!?` |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 29 Jun 2013 12:35 PM |
function explode(div,str) -- credit: http://richard.warburton.it if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 12:51 PM |
| ^ basically str:gmatch("[^{multiple characters}]+") |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 01:02 PM |
wouldn't this be better, though?
function explode(str,div) local t={} str:gsub("(.-)"..div,function(x)t[#t+1]=x end) t[#t+1]=str:reverse():gmatch("(.-)"..div:reverse())():reverse() return t end |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 29 Jun 2013 01:29 PM |
that would actually be awful.
this would be better:
do local t={} local a=function(x)t[#t+1]=x end function explode(str,div) t={} str:gsub("(.-)"..div,a) t[#t+1]=str:match(".*"..div.."(.*)") return t end end |
|
|
| Report Abuse |
|
|