|
HOME
pattern_matching
strsub
tables_and_arrays
email plop
|
. all characters
%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation 0
%b<> matches anything between the < >, they can be replaced by anything you want
An upper case version of any of the above represents the oposite of the class.
For instance, %A represents all non-letter characters.
except %b they all match 1 character, if you want more you can use the following:
+ 1 or more repetitions (returns nil if not found)
* 0 or more repetitions (returns "" on 0 repetitions)
- also 0 or more repetitions (returns "" on 0 repetitions)
? optional (0 or 1 “repetitions”) (returns "" on 0 repetitions)
Some characters, called magic characters, have special meanings when used in a pattern.
The magic characters are:
( ) . % + - * ? [ ^ $
The character % works as an escape for those magic characters.
So, %. matches a dot, and %% matches the % itself.
You can use the escape % not only for the magic characters, but for any non alphanumeric character.
When in doubt, play safe and put an escape.
time to explain a bit more about the so called magic characters.
here they all are again.
( ) . % + - * ? [ ^ $
. + - * ? if you don't know what these do i suggest you read the above post again.
( ) are always used in a pair.
s,e,cmd = strfind(data, "%b<>%s+(%S+)") is something you can find in nearly every script for ptokax.
it means that we wanne give variable cmd the vallue of what is found between the ().
but thats not all, you can use one search to get lots of vallue's like that.
s,e,var1,var2,var3=strfind(data, "%b<>%s+(pat1)%s+(pat2)%s+(pat3)")
now you can see the order in this is straigth forwarded, var1 gets the vallue found by patern 1 etc..
^ now this one can do to things but gone explain only one of them here right now.
on a pattern like this "^%b<>%s+(%S+)" it means it has to start right at the beginning of the string.
if %b<> isn't found it's gone return nil.
$ works in the oposite way was ^.
"%b<>%s+(%S+)$" means start searching from the end of the string and just like ^ it's gone return nil if the first thing in the pattern isn't found.
lets show this with an example script.
code:
string = "why is ptokax is way cooler then yhub? because it can do scripting"
print(" ")
print("the full string were using is: \""..string.."\"")
print(" ")
print("1st searching for the word \"why\"")
s,e,tmp =strfind(string, "(why)")
print("the pattern \"(why)\" returns: "..tmp)
s,e,tmp =strfind(string, "(why)$")
print("the pattern \"(why)$\" returns: "..(tmp or "not found"))
s,e,tmp =strfind(string, "^(why)")
print("the pattern \"^(why)\" returns: "..(tmp or "not found"))
print(" ")
print("now lets do the same patterns on the word \"cooler\"")
s,e,tmp =strfind(string, "(cooler)")
print("the pattern \"(cooler)\" returns: "..tmp)
s,e,tmp =strfind(string, "(cooler)$")
print("the pattern \"(cooler)$\" returns: "..(tmp or "not found"))
s,e,tmp =strfind(string, "^(cooler)")
print("the pattern \"^(cooler)\" returns: "..(tmp or "not found"))
print(" ")
print("now lets do the same patterns on the word \"scripting\"")
s,e,tmp =strfind(string, "(scripting)")
print("the pattern \"(scripting)\" returns: "..tmp)
s,e,tmp =strfind(string, "(scripting)$")
print("the pattern \"(scripting)$\" returns: "..(tmp or "not found"))
s,e,tmp =strfind(string, "^(scripting)")
print("the pattern \"^(scripting)\" returns: "..(tmp or "not found"))
% this is also not new, you've seen it in part 1, it's a so called escape.
we used it before in the above post on things like %S
incase we wanne find one of the magic characters were gone need this one to.
for example we wanne find the % itself we have to escape it from being magic.
sounds more complex then it is, %% this is all.
now you can guess what it needed to find the ?, again simply put a % infront of it, %? and were done.
again a simple example script.
code:
string = "why is ptokax is way cooler then yhub? because it can do scripting"
print(" ")
print("the full string were using is: \""..string.."\"")
print(" ")
print("now lets lets find the ? in the string, and 2 make it easy the word before it")
s,e,tmp = strfind(string, "(yhub%?)")
print(tmp)
[ this one can do real magic.
we know that %d numbers and %a letters but what if we don't wanne find the whole range?
then this is gone be your best friend, but just like () he has a brother which he always needs, the ].
you can make your own ranges with this baby.
lets start with a example script to show the basic, this time not by using strfind but gsub (replace what is found)
first without the magic [ ] then with so we can clearly see the difference.
code:
string = "why is ptokax is way cooler then yhub? because it can do scripting"
print(" ")
print("the full string were using is: \""..string.."\"")
print(" ")
print("now lets lets replace word yhub so we don't have to see that, for the letter \"x\"")
print("first without the magic [ ]")
tmp = gsub(string, "yhub", "x")
print(tmp)
print("now lets lets replace all the letters which make up the word yhub for the letter \"x\"")
print("now with the magic [ ]")
tmp = gsub(string, "[yhub]", "x")
print(tmp)
you can see the difference, the first only matches the whole word, the last takes induvidual characters.
but we can do more then this with it.
promised you i would return to the ^, and now is that time.
just like %S is the oposite of %s, we can use ^ to flip around the magic between [ ].
again a example script.
code:
string = "why is ptokax is way cooler then yhub? because it can do scripting"
print(" ")
print("the full string were using is: \""..string.."\"")
print(" ")
print("now lets lets replace all but the letters which make up the word yhub for the letter \"x\"")
print("now with the magic [ ]")
tmp = gsub(string, "[^yhub%s]", "x")
print(tmp)
that was all for now, i hope you can understand this at least a bit better now.
plop |