From previous readings/study Best practice:

Chapter 17 - JavaScript & PHP Validation aka "Regular Expressions"

Your text says:

A regular expression is an expression that describes a set of strings -- and can be used to determine membership, or not, in a set.

With respect to regular expressions:


Back to the ol' Unix drawing board:

pipe's and bang's

           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}'.

More regular expressions in grep

You may need to add beginning and end of line anchors (^ and $) to make sure you're not just matching substrings. (This is again the difference between regular expressions as definitions of regular languages, and regular expressions used in search.)
----------------------
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?

php version

preg_match($pattern, $subject)



      $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>";
      }


MetaCharacters (sorta like Wildcards) (some but not all useful with ls but good for php!):
? - 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".

Fuzzy Characters
The (.) dot matches anything except a newline so you have to escape it if you are really looking for a dot.

/<.*>/     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

yeahhhhh! (999-99-9999) is a MATCH to our pattern


From this source, how about credit card numbers...

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.

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.

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.

  • js-justJS.html