Welcome, Guest. Please login or register.
Did you miss your activation email?
02/07/12, 08:29
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  The Library
| |-+  Technical Reference Area (Moderators: Flash-db, Musicman, BurtonRider1983, vesa kortelainen, Ronald Wernecke, Jorge Solis)
| | |-+  AS3.0 Regular Expressions (Still being written....)
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: AS3.0 Regular Expressions (Still being written....)  (Read 9224 times)
BurtonRider1983
Global Moderator
Systems Administrator
*****
Posts: 864

Rider-4-Life

BurtonRider1983
View Profile WWW
« on: 08/09/06, 18:28 »

PLEASE READ: This is not yet finished.  I am currently writing the rest of it and working on it periodically.  Check back for more soon.
For conversation on AS3.0 topics check these two threads on the General Board...
http://www.flash-db.com/Board/index.php?topic=15700.0
http://www.flash-db.com/Board/index.php?topic=13780.0
*******************************************************************************************************
Instead of the good old oversized if/else statement, the use of regular expressions can conveniently compact many lines of code into a small single expression.  Regular expressions are great for text  modification (user input formatting, textfile/datafile input formatting, etc...) and verification (email addresses, phone numbers, web address, etc...).


public RegExp(stringRegExp, [flag])

Constructors
There are two forms of constructors for regular expressions.  The first is the java style via the new constructor.
Code:
var myRegExp:RegExp = new RegExp(stringRegExp, [flag]);

The first parameter that you pass the constructor is a string regular expression.  The second parameter is the optional flags.

The second constructor is by simply setting it equal to a string...
Code:
var myRegExp:RegExp = /test/i;
The difference is that you are only able to pass it one parameter and it is not inside quotes. In order to encorporate the flag there is a slighly different format for your regular expression.  You must encapsulate your regular expression in forward slashes ("/").  If you want to use a flag you will need to place if after the encapsulated regular expression as shown above.



Flags
"s" - dotall:boolean; when true the period in the regular expression matches the newline character in the string being searched.
"x" - extended:boolean; when true white spaces in the string constructor are ignored.
       Extended mode is used when you want your regular expressions to be easily understood.  You can use whitespace to break
       up your regular expression and it will be ignored.
"g" - global:boolean; This flag is used when you wish to use the lastIndex property of the class RegExp.
       lastIndex is used to search for more than one occurance of your regular expression pattern in the string. 
Code:
var myRegExp:RegExp = /test/ig;
var myString:String = "This is my test string that I am searching through. Test. Testing. Today is Tuesday, yesterday was Monday, tomorrow is Wednesday. Tester.";
var myObjectResult = myRegExp.exec(myString);

while(myObjectResult != null){
    trace(myObjectResult);
    myObjectResult = myRegExp.exec(myString);
}
    This code will loop through and return...
    test
    Test
    Test
    Test
    The reason for this output is that we were looking for the string "test" no matter what case.  It returns the string we searched for. 
    If you would like to return the whole word use this code for your RegExp....
Code:
var myRegExp:RegExp = /test\w*/ig;
    That will output...
    test
    Test
    Testing
    Tester

"m" - multiline:boolean; This flag is used to indicate multiline strings will be used. 
        This does not have to be used but it allows for the use of the "^" and "$" ancors. They signify before and after the newline.
Code:
var myRegExp:RegExp = /^test\w*/igm;
var myString:String = "This is my test string that I am searching through.\n" + "Test. Testing. Today is Tuesday, yesterday was Monday, tomorrow is Wednesday. Tester.";
trace("myRegExp: " + myRegExp);
var myObjectResult = myRegExp.exec(myString);
while(myObjectResult != null){
    trace(myObjectResult);

    // Attempt to find the next string match
    myObjectResult = myRegExp.exec(myString);
}
         ....will output....
         Test

         This is due to the fact that "^" indicates after a newline character and there is only one string that matches our search.
"i" -  ignoreCase:boolean; This flag is set when you want your searches to be non case sensative.



Formulating Regular Expressions
A regular expression is a string of characters.  You are limited as to which characters you may use and there are a few metacharacters and metasequences.

Metacharacters
^ $ \ . * + ? ( ) [ ] { } |
Metacharacters are characters that have special meaning. Above is a list of metacharacters. 
"^" signifies the beginning of the line; when used in conjunction with the multiline flag ("m") and the global flag ("g"), it signifies the beginning of each of the lines. 
      It is sometime referred to as an ancor metacharacter.  When used inside of a character class it holds different meanings. Inside of a character class this metacharacter means "except" all
      characters, or character ranges preceeding it in the character class.  It must be placed at the beginning of the character class or it will be treated as the literal value and not as a
      metacharacter.
Code:
var myRegExp:RegExp = /[^a-z]/;
     The above code creates a regular expression that specifies all characters except for lowercase alpha characters.
Code:
var myRegExp:RegExp = /[^a-z]/i;
var myRegExp:RegExp = /[^a-zA-Z]/;
     The above code shows two ways to search for all non alpha characters.

"/" signifies the beginning and end of a regular expression.
Note: The "/" is not required when constructing your RegExp via the new constructor because you are passing it a string and it is converting that string for you into your regular expression.

"[]" - creates a character class. 
Note: Inside character classes metacharacters and metasequences that have special meaning outside, loose their special meaning. An example would be the Quantifiers (listed below).  The asterisks ("*") means zero or more occurance outside of the character class but inside of the character class it is a literal asterisk.

"-" - Placed inside of a character class this metacharacter specifies a range.  You must have a character or metasequences ("\unnnn" or "\xnn": see below metasequences). 
Code:
var myRegExp:RegExp = /[a-z]/;
Above creates a regular expression that specifies a range of characters including all lowercase alpha characters.

Quantifiers
A quantifier is a special metacharacter that signifies a number of occurances of a character, character class, or group.
"*" - match the previous character, character class, or group zero or more times.
Code:
// This will cause an error when you compile!
var myRegExp:RegExp = /[t]*/ig;
The above regular expression would read "zero or more t's (T or t)".  It may read that way, but it will not work.  When using the asterisks quantifier you must have another character, character class, or group, otherwise AS sees the string as always having zero or more of the character.  I assumed it would just return all of the characters found and stop after looping through the last char in the string, but this is not the case.  It causes an infinite loop and Flash will stop it after the timeout period (default 15 seconds).
Code:
// Adding code later...
"+" - matches the previous character, character class, or group one or more times.
"?" - matches the previous character, character class, or group zero or one times.
"{n}" - matches the previous character, character class, or group n times.
"{n, }" - matches the previous character, character class, or group n or more times.
"{n1, n2}" - matches the previous character, character class, or group n1 to n2 times.

Metasequences
"\d" - matches any digits (0-9)
"\n" - matches the newline
"\r" - matches a return
"\t" - matches a tab character
"\unnnn" - matches the character represeted by the Unicode point value of 'nnnn' (where nnnn is a hexadecimal value).
"\xnn" - matches the character represented by the ASCII value of 'nn' (where nn is a hexadecimal value).

Methods of the RegExp class.
public function exec(testString:String):Object;
This method searches accepts a string as a parameter.  It preform a search for the regular expression on the string.  It will return an object which I will give a breakdown of or if the search comes up negative, null is returned.

Code:
var myRegExp:RegExp = new RegExp("test", "i");
var myString:String = "This is my test string that I am searching through. Test. Testing. Tester.";
trace("myRegExp: " + myRegExp);
var myObjectResult = myRegExp.exec(myString);

public function test(testString:String):boolean;
// Coming soon
« Last Edit: 08/12/06, 14:49 by BurtonRider1983 » Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
anything