generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Help with String Patterns

Previous Thread :: Next Thread 
Crafted is not online. Crafted
Joined: 13 Jul 2009
Total Posts: 740
18 Sep 2015 06:06 PM
if I have tile[1][2] how do I get 1 in an x variable and 2 in a y variable?

pattern = "%d" will give me 1, but what if I want to get the 2?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Sep 2015 06:08 PM
blah = "[123][456]";
local x, y = blah:match("%[(%d+)%]%[(%d+)%]");

Although if there might be non-whole numbers, it might be better to match everything in the brackets.
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
18 Sep 2015 06:08 PM
for thing in string.gmatch(s,"%d") do
--blah
end
Report Abuse
Crafted is not online. Crafted
Joined: 13 Jul 2009
Total Posts: 740
18 Sep 2015 06:13 PM
cntkillme's method worked perfectly, though I'm not exactly sure why.

the string patterns are so confusing.
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
18 Sep 2015 06:13 PM
he hacks


while true do ur mom
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Sep 2015 06:15 PM
I'll explain it as well as I can without getting into too much detail

blah = "[123][456]";
local x, y = blah:match("%[(%d+)%]%[(%d+)%]");

Because '[' and ']' are special, we need to 'escape' them (treat them as a regular character we want to look for) so doing %[ will escape it (and %] as well).

Anything in parenthesis (called captures) are returned, and since I have two captures, 2 values are returned (x and y, respectively).

So in the brackets, I search for 1 or more digits, and put them around parenthesis. I do that twice to get the x and y.

In short, I capture a sequence of digits )twice) in brackets and return them.
Report Abuse
Crafted is not online. Crafted
Joined: 13 Jul 2009
Total Posts: 740
18 Sep 2015 06:22 PM
I see what your saying.

One more question:

Why do (%d+) instead of (%d)?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Sep 2015 06:24 PM
%d will only match a single digit, I want to match 1 or more.
Report Abuse
Crafted is not online. Crafted
Joined: 13 Jul 2009
Total Posts: 740
18 Sep 2015 06:26 PM
But in a case were the only possible values of each are a single digit (1-9) it would be find just to use %d?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Sep 2015 06:27 PM
Yeah, honestly you can actually just do this if that's the case:

x, y = match("(%d).-(%d)")
Report Abuse
Crafted is not online. Crafted
Joined: 13 Jul 2009
Total Posts: 740
18 Sep 2015 06:28 PM
Alright, thanks for the help!!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image