Regular Expressions Tutorial
Regular Expressions Tutorial
Notes: Symbols refer to specific things. Use \ to refer to literal characters (escaping). Use the g flag to continue searching after the first match (global). Spaces are characters too. Multiline flag is used when working with the beginning and end of a string.
Symbol . ?
[]
() + * |
\d \w
\s \r
What It Means Matches any character, except for a line break. Use only as a last resort. Used to specify that the preceding character or token is optional. It will match 0 or 1 of the preceding token. Example: colou?r Matches color and colour. Match any single character in the set. Used to specify a list of characters to match one of. Also called character class. Example: Defen[cs]e Matches defense and defence. Used to wrap a set of characters. Matches 1 or more of the preceding token. This is a greedy match, and will match as many characters as possible before satisfying the next token. Matches 0 or more of the preceding token. Equivalent of OR. Matches the full expression before or after the |. Example: pear|kiwi|plum Matches pear, kiwi or plum. Used to look for a character that occurs at the beginning of the string. Example: ^[A-Z] Matches a capital letter at the beginning of a string. Used to look for a character that occurs at the end of the string. Example: (jpg|png|gif)$ Matches picture extension. Matches any digit character (0-9). Matches any word character (alphanumeric & underscore). Note: The actionscript implementation of word characters is very poor, and does not include any extended word characters, such as accented characters. Matches space, including line breaks. Matches carriage return character.
Matches new line character. Matches previous token between m to n times. This is a greedy match, and will match as many characters as possible. Matches characters in the range a-z Matches character that are not a, b or c
Capturing Matches Example: Regular Expression: ^.+(jpg|gif|png)$ Replace With: HELLO Subject String: myfile.jpg greatPhoto.jpg greatPhoto.exe anotherImage.gif greatPhoto.app ReallyCooljpg.jpg my.jpgimage.jpg Test Results: HELLO HELLO greatPhoto.exe HELLO greatPhoto.app HELLO HELLO Greedy Matches Example: H.+H Hello how are you Hello. And he said,"Hello to you!" H.+?H Hello how are you Hello. And he said,"Hello to you!"