Teaching Regular Expressions (Regex) - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22) +---- Thread: Teaching Regular Expressions (Regex) (/showthread.php?tid=3343) |
Teaching Regular Expressions (Regex) - Project Evolution - 12-05-2009 Note: Sorry if I am unclear with somethings I say; when I am teaching things I tend to explian things abstractly. A regular expression is a sort of pattern-matching mini language. Regex can be very useful for anythign using string manipulation such as filtering certain characters, matching, or input validation. It can also be used for searches! In this tutorial, you are going to learn the basics of regex and hopefully how to work off this sort of "mini language." Be aware that the actual regex syntax is different than Java. Here is an example of regex pattern matching; Code: Enter Regex: abc Regex lets you use two types of character classes; predefined and custom classes. Self explanatory, predefined is what is already made, and custom is what you want the matcher to specifically find. Here's a table of predefined characters: The period (.) is a wildcard that will match any character: Code: Enter Regex: b.t The \d class like said in the table is for any digit (0-9). Here is an example how to use digits in a phone number format: Code: Enter Regex: \d\d\d - \d\d\d - \d\d\d\d I will lastly explain the \s class since the others you should have an idea of. Remember that the \s class matches white space characters. Heres an example: Code: Enter Regex: ...\s... If you simply want to limit the pattern to match only one space, use this: Code: Enter Regex: ... ... Now onto some custom character classes, its hardly different from basic character matching using predefined classes, but still very effective. To create a custom character class, place the characters you wish in a set of brackets like so: Code: Enter Regex: b[aeiou]te You can also use more than one set of brackets like so: Code: Enter Regex: [bB] [aeiou] [tT] Custom character classes can also specify ranges given inside the brackets. For example: Code: Enter Regex: [a-z] [0-5] You can also range more than once class: Code: Enter Regex: [a-zA-Z0-9] Regex can include classes that can match any character but ones preceeding a caret(^) as shown: Code: Enter Regex: [^cf]at Stay tuned as I will be posting an advanced tutorial of regular expressions. I hope you learned something new today. ~ Project Evolution RE: Teaching Regular Expressions (Regex) - nevets04 - 12-05-2009 Looks like a good tutorial, I'll give it a read when I get home from my driving lesson. RE: Teaching Regular Expressions (Regex) - Project Evolution - 12-05-2009 Sure thing, im going to be including the advanced version soon which will come with a sample program as to some good uses for regex. |