Mel: Validating Input Data

From wikinotes

Gmatch

A slightly more powerful version of match. Returns a boolean instead of matching string.


Match

Match

Match searches a string for a specific group of characters

match "(blah)" "abcblahde";				//returns blah if it finds the word blah, or an empty string if it does not

Variations of Match

match "(blah)+" "ablahblahblah29eblah";		//Result: blahblahblah 		//returns the first chained blahs (blahblahblah)

match "(blah)*" "blahblahabc";			//Result: blahblah		//only returns blahs at teh start of the string

match "[abc]+" "tqfmabg";			//Result: ab			//returns the first a, b, or c and if they are
										//together then returns them that way


match "[a-c]+" "tqfmab"				//Result: ab			//this looks for letters in the range of a to c
										//combined with the effects of the previous command


match "[A-Z]" "tqfmBj"				//Result: B			//looks for capital letters from A to Z


match ".+" "tqi"				//Result: tqi			//returns All characters


Match + strcmp

The first two lines create a string then perform a match. The third line uses strcmp. It checks to see if two values are equal or not, and returns a 0 or -1 for equal or not equal respectively.

from my experimentation, it seems that strcmp will return a:

  • 1 if the first word (blah) contains more characters than the second
  • -1 if the first word (blah) contains less characters than the second
  • 0 if the two words contain the same number of characters
string $mystring = "1blah23";
string $matchesBlah = `match "blah" $mystring`;
int $foundmatch = `strcmp "blah" $matchesBlah`;


Tokenize

Tokenize will divide a string by a single character and store it's chunks in an array. Tokenize with multiple characters will search for any individual instance of a character. ex: tokenize $mystring "ab" $buffer; would remove any "a"s or "b"s and not search for "ab"s.

string $mystring = "myObject.translateX.89";
string $buffer[];

tokenize $mystring "." $buffer;


//Result://

buffer[0] == "myyObject";
buffer[1] == "translateX";
buffer[2] == "89";

substituteAllString

substituteAllString replaces every instance of a string with another string. You can use multiple characters.

string $origString = "C:/Users/Will/Desktop"

substituteAllString ($origString, "/", "_")


//Result:// C:_Users_Will_Desktop

startsWith

Query the first few letters of a variable or test what the first few letters are.

//Testing first characters:
startsWith("abc","ax"); == 0
startsWith("abc","ab"); == 1

//Querying characters
startsWith("abc",1); == a
startsWith("abc",2); == ab