Thursday, September 22, 2011

Regular expressions table



The Perl You Need to Know: Basic Regular Expression Syntax
.
any single character The dot (.) can be used as a placeholder for any character. Examples:

"do." would match "dog", "dot", "doe", etc.
"d..r" would match "door" and "deer".
*
zero or more of the previous character The asterisk (*) specifies that zero or more instances of the previous character should exist in sequence. Examples:

"do.*" would match "dog", "done", "doppleganger", etc.
(why? "d-o- followed by zero or more of any chararcter")

"to*" would match "to" and "too"
(why? "t-o- followed by zero or more o's")

"fre*.." would match "frat", "free", "from"
(why? "f-r- followed by zero or more e's followed by any two characters)
+
one or more of the previous character The plus sign (+) demands that there be at least one of the previous character in sequence; similar to (*) but slightly more strict. Examples:

"fre+.." would match "freak", "freeze", "fresh"
(why? "f-r- followed by one or more e's followed by any two characters)
?
zero or one of the previous character The question mark (?) says that there should be zero or one of the previous character but not more than one. This is stricter than either (*) or (+). Examples:

"ton?e" would match "toe" and "tone"
(why? "t-o- followed by zero or one n followed by e")
( ) grouping The parentheses ( ) are used to group together patterns, for instance, to logically combine two or more patterns. Example:

(dog|cat) would match "dog" and "cat"
(why? "dog or cat")
[]
any character from the set The square brackets ([]) can be used as a placeholder for a single character which matches any of a set of characters. Confusing, at first, but some examples should clarify:

"ta[pb]" would match "tap" and "tab"
(why? "t-a- followed by one character from the set of pb")

"r[aeiou]t" would match "rat", "ret", "rot", "rut"
(why? "r- followed by one character from the set of vowels followed by t")

"r[aeiou]+t" would match "rat" (plus all of the above), "riot", "root", etc.
(why? "r- followed by one or more vowels followed by t")
[^]
any character not from the set Placing a carat (^) inside the square brackets ([]) negates the set; meaning the character must match any character not within the set. This is a useful way of specifying a large set of characters, for instance, consonants are "not vowels"; examples:

"t[^aeiou]+.*s" matches "thanks", "this", "trappings", etc.
(why? "t- followed by one or more of any character which is not a vowel followed by zero or more of any character followed by an s")
{min,max} range of occurrences The curly braces ({}) are used to require that the preceding character or set of characters occur a certain number of times. Examples:

"[a-z]{3}" would require that a lowercase letter appear 3 consecutive times.


"[0-9]{3,}" would require that a digit appear 3 or more consecutive times.

"[A-Z]{2,5}" would require that an uppercase letter appear between 2 and 5 consecutive times.


Character Classes Anchor Sequences
\d Any digit [0-9] ^ Beginning of data string
\D Any non-digit [^0-9] $ End of data string
\w Any alphanumeric [a-zA-Z0-9_] \b A word boundary
\W Any non-alphanumeric [^a-zA-Z0-9_] \B Any place except a word boundary
\s Any space [ \t\n\r\f]    
\S Any non-space [^ \t\n\r\f]    


Escape Sequences
\n Newline character, aka linefeed. This is the typical end-of-line character.
\r Carriage return character.
\t Tab character.
\e Escape character.
\xFF A hexadecimal value in place of "FF".

Sunday, September 19, 2010

Unpack javascript files from compresed format

Hi javascriptians,
You will have at some stage come across a piece of Javascript code that has been packed to compress as well as to obfusicate the code.  If you want to get the original code from the compressed files you can use the another unpacking javascript code. I am getting this code from http://matthewfl.com/unPacker.html  writen by mathew. I got this and used very much, so I wanted to share with my friends.


Thanks to Mathew

unpacker.js
-----------

//////////////////////////////////////////
//  Un pack the code from the /packer/  //
//  By matthew@matthewfl.com            //
//  http://matthewfl.com/unPacker.html  //
//////////////////////////////////////////
// version 1.2


function unPack (code) {
function indent (code) {
try {
var tabs = 0, old=-1, add='';
for(var i=0;i<code.length;i++) {
if(code[i].indexOf("{") != -1) tabs++;
if(code[i].indexOf("}") != -1) tabs--;

if(old != tabs) {
old = tabs;
add = "";
while (old > 0) {
add += "\t";
old--;
}
old = tabs;
}

code[i] = add + code[i];
}
} finally {
tabs = null;
old = null;
add = null;
}
return code;
}
  
    var env = {
        eval: function (c) {
            code = c;
        },
        window: {},
        document: {}
    };
  
    eval("with(env) {" + code + "}");

code = (code+"").replace(/;/g, ";\n").replace(/{/g, "\n{\n").replace(/}/g, "\n}\n").replace(/\n;\n/g, ";\n").replace(/\n\n/g, "\n");

    code = code.split("\n");
    code = indent(code);
  
    code = code.join("\n");
    return code;
}


Save this file in unpacker.js

Example how to use
include the javascript file in a html page using
<script src="unpacker.js" ></script>

and call the function unPack() to unpack the code like following

<script>
   code = "<unpacked file contents>"
    var unpackedcontent = unPack(code);
   document.write(unpackedcontent); // optional, to show the unpacked content.
</script>


If you have any issues, please feel free to contact me at codesmokers@gmail.com

Send attachment mail using php

Hi php folks,

If you would like to send a mail thru php, it is very easy,
You can send mail from online,
but If you want to send an attachment in php mail function, its somewhat difficult.

I have added some sample code to send an attachment in php mail

Create a file attachment.pdf which is going to be attached with the mail

And then use the following code in a php file

mailattachment.php
~~~~~~~~~~~~
<?php

    $to = "toaddress@domain.com";
    $subject = "Subject for mail attachment";

    $random_hash = md5(date('r', time()));

      $headers = "From: fromemail@domain.com\r\nReply-To: noreply@domain.com";
    $headers .= "\r\nMIME-Version: 1.0";
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  
    $attachment = chunk_split(base64_encode(file_get_contents("attachment.pdf")));

  $output = "--PHP-mixed-$random_hash;

Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello World!
This is the simple text version of the email message.

--PHP-mixed-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>


--PHP-mixed-$random_hash
Content-Type: application/pdf; name=attachment.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$random_hash--";

  echo @mail($to, $subject, $output, $headers);

?>


Change the content type to appropriate file type and make sure  the attachment file is in correct location 
example: for pdf  Content-Type: application/pdf;
for gif : Content-Type: application/gif;
for text: Content-Type: application/plain; 


Save the code in a file mailattachment.php and run it from the php server,
and check the inbox to see the attachment.

If you have any queries, please contact me thru mail
codesmokers@gmail.com