|
| 18 Dec 2015 11:32 AM |
Say I have a string,
x = "This is my string"
How would I remove the last word from that, and reprint it? |
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 18 Dec 2015 11:43 AM |
print(x:gsub("%a-$",""))
it would stop at anything that's not a letter though
|
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 18 Dec 2015 11:45 AM |
print(x:gsub(" [%a%d]-$",""))
that would remove the first whitespace found and all the letters & digits |
|
|
| Report Abuse |
|
|
|
| 18 Dec 2015 12:37 PM |
Ozzy, just use %w, it matches letters and numbers. e_e
But frankly, I would go with %S+$ it matches any character that isn't a whitespace. |
|
|
| Report Abuse |
|
|
|
| 18 Dec 2015 12:43 PM |
Thanks! However, if I have a string such as x = "Blue Wooden Maru Katana 3565232" How do I simply print, Blue Wooden Maru Katana, without the numbers at the end? |
|
|
| Report Abuse |
|
|
b6e
|
  |
| Joined: 14 Nov 2014 |
| Total Posts: 33 |
|
|
| 18 Dec 2015 12:46 PM |
local name, otherstuff = x:match("^(.-)%s-(%d*)$")
-yo recipes
|
|
|
| Report Abuse |
|
|
|
| 18 Dec 2015 12:57 PM |
| That just printed the number at the end, not the words before the number? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Dec 2015 02:08 PM |
x = "Blue Wooden Maru Katana 3565232" x = x:match("(.-)%S+$"); print(x) |
|
|
| Report Abuse |
|
|
|
| 18 Dec 2015 02:48 PM |
| "x = x:match("(.-)%S+$")" Is this new? |
|
|
| Report Abuse |
|
|
b6e
|
  |
| Joined: 14 Nov 2014 |
| Total Posts: 33 |
|
|
| 18 Dec 2015 03:41 PM |
@cnt that's just going to print an empty string as %S+ is greedy and .- is not
-Commit committed....
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Dec 2015 04:04 PM |
You're stupid, you don't understand how patterns work when you assume you do so you don't bother to test it. (.-) (.*) or (.+) all work here and it's obvious why. Mine _will_ print "Blue Wooden Maru Katana" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Dec 2015 04:05 PM |
| @GRAGGER the match function is old |
|
|
| Report Abuse |
|
|