combined with a generally suspicious & pessimistic nature!
With respect to regular expressions:
ps -aelf | grep 'ldale' grep "famous last words" *.txt | grep "fred" ---------------------- OS's come with words lists used by various programs to check spelling. On Unix-flavor systems, We can use Unix command 'locate' to find files with 'word' as part of their name and then filter for only the ones that are also... dictionary related. locate words <----------------- returns a real mess'o'info! locate words | grep dict <----------------- ahh, a thing of beauty vi /usr/share/dict/words <----------------- vi one of them and see what's there... ls /usr/share/dict <----------------- or maybe see what else is in the folder... ls -la /usr/share/dict <----------------- and to see what's with the lil @ symbols How do you spell .... that which makes us civil? grep glory$ /usr/share/dict/words <----------------- find all the words that END($) in 'glory' grep ^morning /usr/share/dict/words <----------------- find all words that START(^) with 'morning' ^...$ begin...end grep ^mor /usr/share/dict/words | grep y$ <----------------- find all that START(^) with 'mor' & END in 'y'; pipe'd together grep ^mor[A-Za-z]*y$ /usr/share/dict/words <----------------- or all in one regular expression grep -c ^mor[A-Za-z]*y$ /usr/share/dict/words <----------------- just tell me how many (-c for 'count') egrep is the same as grep -E. Both indicate that "Extended Regular Expressions" should be used instead of grep's default Regular Expressions. grep requires the backslashes for plain Regular Expressions. egrep does not! From the man page: Basic vs Extended Regular Expressions In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \). Ok, but WHAT does all this MEAN?!? egrep '^(a)\1{1}' /usr/share/dict/words <----------------- words that begin with 2 of the letter a egrep '^(.)\1{1}' /usr/share/dict/words <----------------- This is the 'answer' to number 1) below, 2 of any character. Use egrep '(.)\1{N}' and replace N with the number of characters you wish to replace minus one (since the dot matches the first one). So if you want to match a character repeated four times, use egrep '(.)\1{3}'.
---------------------- Try this? Once you have located your system word list file, write a program that will read lines from the file and, using a suitable regex, display all the words that: 1. begin with a pair of repeated letters 2. end in "gory" 3. have more than one pair of repeated letters 4. are palindromes 5. consist of letters arranged in strictly increasing alphabetic order (e.g., knot) ----------------------
Where do we find regular expressions implemented/used/useful?
$pattern ='/^\d{3}-\d{2}-\d{4}$/'; $theGood="999-99-9999"; // good; should match $theBad ="9a9-99-9999"; // bad; that 'a' is not ok! $subject=$theBad; if ( preg_match($pattern, $subject) ){ echo "<h2 style='color:green'>yeahhhhh! ($subject) is a MATCH to our pattern</h2>"; }else{ echo "<h2 style='color:red'>($subject) is NOT a match.</h2>"; }
? - zero or one of preceding element ie, colou?r matches both "color" and "colour". * - zero or more of preceding element ie, ab*c matches "ac", "abc", "abbc", "abbbc", and so on. + - one or more of preceding element ie, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
/<.*>/ versus /5\.0/
Grouping, Character Classes & Ranges(like all digits \d and some[3-4]), and more... modifiers, like we saw with /g
Verify that a string is a social security number of the format ddd-dd-dddd
With a few simple regular expressions, you can easily verify whether your customer entered a valid credit card number on your order form. You can even determine the type of credit card being used. Each card issuer has its own range of card numbers, identified by the first 4 digits. Visa: ^4[0-9]{12}(?:[0-9]{3})?$ All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. MasterCard: ^5[1-5][0-9]{14}$ All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. American Express: ^3[47][0-9]{13}$ American Express card numbers start with 34 or 37 and have 15 digits. Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard. Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ Discover card numbers begin with 6011 or 65. All have 16 digits. JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits. With a few simple regular expressions, you can easily verify whether your customer entered a valid credit card number on your order form. You can even determine the type of credit card being used. Each card issuer has its own range of card numbers, identified by the first 4 digits. You can use a slightly different regular expression to find credit card numbers, or number sequences that might be credit card numbers, within larger documents. This can be very useful to prove in a security audit that you're not improperly exposing your clients' financial details.
Visa: ^4[0-9]{12}(?:[0-9]{3})?$ All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. MasterCard: ^5[1-5][0-9]{14}$ All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. American Express: ^3[47][0-9]{13}$ American Express card numbers start with 34 or 37 and have 15 digits. Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard. Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ Discover card numbers begin with 6011 or 65. All have 16 digits. JCB: ^(?:2131|1800|35\d{3})\d{11}$ JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits.
Luhn's Algm for validation of credit card numbers
Modern databases often offer built-in regular expression features that can be used in SQL statements to filter columns using a regular expression. With some databases you can also use regular expressions to extract the useful part of a column, or to modify columns using a search-and-replace. MySQL - MySQL's REGEXP operator works just like the LIKE operator, except that it uses a POSIX Extended Regular Expression.