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 » Scripting Helpers
Home Search
 

Re: string patterns...

Previous Thread :: Next Thread 
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
25 Feb 2012 11:30 PM
yo, wazzap SH. I've helped you a lot so now it's time you help me!
can someone explain string patterns to me? I have tried to understand wiki's article but it wasn't in depth enough for me... (unlike most of the other wiki articles :D)
Report Abuse
thek00lkid is not online. thek00lkid
Joined: 18 Jun 2011
Total Posts: 2778
25 Feb 2012 11:37 PM
They're pretty much commands, I never use them and you probably won't. Most programming languages have them. They simply edit Strings. Even the concat operator can be considered a string pattern. To be more specific, you'd have to ask which one you're struggling with. Though I don't really use any of them, except concat, so don't ask me. =3
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
25 Feb 2012 11:39 PM
what I mean is like how does the pattern stuff work with "%S+"
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
25 Feb 2012 11:40 PM
and that was just an example. another thing i took from wiki:
"(%a+)%s?=%s?(%d+)"

that stuff looks really complicated O_O


im trying to optimize my admin commands so i think that string patterns will come in handy...
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
26 Feb 2012 12:20 AM
Haha. This is a very useful page, that I'm sure you've already seen. >.>

http://wiki.roblox.com/index.php/String_Patterns

Look at the table of character classes.
Also, look at the quantifiers table.

Finally, know that capitalizing the pattern makes it reverse. When a pattern would check for a symbol, the capitalized version would check for everything except for the pattern.

If you want to use these in your commands, I would recommend using a for (x) in string.gmatch() loop. :P
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 12:46 AM
Well blue, that pattern example is horrible. Unless you need something like that, just ignore it. I will give you the basic run down...


Patterns are very useful, they are able to get a value AFTER a character. There are many different types of pattern identifiers, some for example:

%s - Spaces
%a - Alphabetical (a-z, capital,not capital)
%d - Digits

Just look at the first part in the pattern article, shows all the patterns. Then there are captures they are represented by an opening "(". These are useful if you want to capture(Get) a value at a certain position. Here is a simple example...


string.match("Hi Blue!$~!@#%!#$%!#$%!@#$!@#","Hi (%a)!$~!@#%!#$%!#$%!@#$!@#")

That simple example will return "Blue", which is between hi and all the punctuation. So it will capture anything in-between the two things mentioned (Hi and punctuation).

Here is an example aimed towards a kill command:

*Assuming the msg and all that is sorted*

if game.Workspace:FindFirstChild(msg:match("kill:","kill:(%a)")) then
game.Workspace[msg:match("kill:","kill:(%a)")]:BreakJoints()
end

That will basically return a name after kill. The third thing is a set, a set basically allows you to use multiple patterns at once and exclude those you do not want to capture. A set is usually included in a capture. A simple example:

local s = "We want to get on digits and Punctuation #!$@#$!@#12341ASDFA"
print(s:match("Punctuation ([%p%d]+)"))

Using a set will allow us to state multiple patterns. I will explain what the + sign is now...


There are four different identifiers

+ -- This will get whatever has 1 or more repetitions and return the longest string
- -- Optional, there does not need to be any occurrence if there is it will return them. It will return the shortest string
* -- Optional, there does not need to be any occurrence if there is it will return them. It is like the + but it does not need an occurrence

? -- Optional, there does not need to be any occurrence if there is it will return them. Will get the first thing found.

Examples of all of them:


local s = "Hai Dere P4rdener"

print(string.match(s,"%a+"))-- Will return Hai since it is the first alphabetical sequence that is matched before the space


print(string.match(s,"%a?")) -- Will return H because it returns the shortest string matched, so H


print(string.match(s,"%a*")) -- Will also print Hai for the same reason as +


That is all I really can explain, sorry if you still do not get it. If you need a basic command script to study I can toss you one that is very in depth.

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 12:46 AM
Note: Do not have the forum enhancer enabled to view what is above.

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
26 Feb 2012 01:02 AM
is there a way of getting numbers/words separated by a certain character? eg:

local msg = "stats:Gold:player:100"

local tab = ???

print(pairs(tab))


>1 stats
>2 Gold
>3 player
>4 100
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 01:52 AM
Yes

but I would suggest just using string.sub probably more efficient.

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 01:52 AM
Here is an example though.

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
26 Feb 2012 01:52 AM
oh, kk i'll forget about string patterns again :) LOL!!!!!!!
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
26 Feb 2012 01:55 AM
Read this: lua dot org slash manual slash 5.1 slash manual.html#5.4.1

It's the Lua 5.1 Reference Manual. I refer to that exact section of it whenever I forget how to use string patterns, because it lists everything I'd need to know.
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 01:55 AM
local msg = "stats:Gold:player:100"
local s1,s2,s4,s5 = msg:match("([%a%d%p%s]*):(%d*):([%a%d]):(%d)")

print(s1,s2,s4,s5)


[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 01:57 AM
Woulda helped to test that before posting -.-

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
26 Feb 2012 01:59 AM
The other example is based on the actual values, this is based on that string given...

local msg = "stats:Gold:player:100"
local s1,s2,s3,s5 = msg:match("([%a%d%p%s]*):(%a*):([%a%d]*):(%d*)")

print(s1,s2,s3,s4,s5)

[[ 7/10 - Scripting| 4/10 - Building | 10/10 - Confused ]]
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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