Chapter 6. The String library

Squirrel Standard Library 3.0

Chapter 6. The String library

Squirrel API

Global symbols

eg.
sq> print(format("%s %d 0x%02X\n","this is a test :",123,10));
this is a test : 123 0x0A

					
\ Quote the next metacharacter
^ Match the beginning of the string
. Match any character
$ Match the end of the string
| Alternation
(subexp) Grouping (creates a capture)
(?:subexp) No Capture Grouping (no capture)
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\l lowercase next char
\u uppercase next char
\a letters
\A non letters
\w alphanumeric [_0-9a-zA-Z]
\W non alphanumeric [^_0-9a-zA-Z]
\s space
\S non space
\d digits
\D non nondigits
\x exadecimal digits
\X non exadecimal digits
\c control charactrs
\C non control charactrs
\p punctation
\P non punctation
\b word boundary
\B non word boundary
              
eg.
local a = split("1.2-3;4/5",".-/;");
// the result will be  [1,2,3,4,5]

					
            

Regexp class

 
local ex = regexp(@"(\d+) ([a-zA-Z]+)(\p)");
local string = "stuff 123 Test;";
local res = ex.capture(string);
foreach(i,val in res)
{
	print(format("match number[%02d] %s\n",
			i,string.slice(val.begin,val.end))); //prints "Test"
}

...
will print
match number[00] 123 Test;
match number[01] 123
match number[02] Test
match number[03] ;
				
				
 
local ex = regexp("[a-zA-Z]+");
local string = "123 Test;";
local res = ex.search(string);
print(string.slice(res.begin,res.end)); //prints "Test"