Showing posts with label Regular Exp. Show all posts
Showing posts with label Regular Exp. Show all posts

Tuesday, July 8, 2014

Bulk rename using bash rename command

Rename command with RegEx pattern can be used to do the bulk rename operation. For example, to append the ip address of the machine in between the log files supplied, the below code is used.
#!/bin/bash
ip=$(hostname -I | sed 's/[^0-9.]*//g')
file="_"$ip"."
rename -v "s/(\w+).(\w+)+/\$1$file\$2/" *.log*
The output will like:
shell.log renamed as shell_192.168.1.2.log
shell.log.1 renamed as shell_192.168.1.2.log.1
shell.log.2 renamed as shell_192.168.1.2.log.2
shell.log.zip renamed as shell_192.168.1.2.log.zip

Tuesday, January 21, 2014

Remove line numbers from source code using sed

While getting sample code from internet, it may comes with line number. To simply remove those lines from the code file use the below ways.

Using sed command with RegEx pattern in Linux, that can be achieved.

To remove it from console:
$ sed -i "s/^ *[0-9.]* *//g" code.txt
To remove it from vim interface:
:%s/^ *[0-9.]* *//g
And the same RegEx pattern can be used in anywhere to remove the line numbers from the source code file.

 

Tuesday, August 30, 2011

Conditional patterns in Regular expression in PHP


PHP code:


$phoneno = array(
"413 222 6859",
"4-(413)-222-6859",
"(413-222-6859",
"(413).222.6859",
"413 22 6859");


foreach ($phoneno as $number) {
echo "$number : ";


if (preg_match("/^


(1[-\s.])? # optional '1-', '1.' or '1'
( \( )? # optional opening parenthesis
\d{3} # the area code
(?(2) \) ) # if there was opening parenthesis, close it
[-\s.]? # followed by '-' or '.' or space
\d{3} # first 3 digits
[-\s.]? # followed by '-' or '.' or space
\d{4} # last 4 digits


$/x",$number)) {  # x to ignore white spaces in pattern


echo "valid\n";
} else {
echo "invalid\n";
}
}


Output:


413 222 6859 : valid
4-(413)-222-6859 : valid
(413-222-6859 : invalid
(413).222.6859 : valid
413 22 6859 : invalid

Using callback function in PHP to replace strings for huge items like cover letter

PHP code:


<?php


//function to replace the patters by exact string values
function my_callback($matches) {
$data = array("name"=>"senthil kumar", "age"=>"29", "work_domain"=>"PHP", "experience"=>"6");


if (isset($data[$matches[1]])) {
// return the replacement string
return $data[$matches[1]];
} else {
return $matches[0];
}
}


// this will call my_callback() every time it sees brackets
$template = "I am [name], age of [age] working as [work_domain] developer for past [experience] year.";
$template = preg_replace_callback('/\[([\w_]+)\]/','my_callback',$template);
echo $template;

?>

Output:

        I am senthil kumar, age of 29 working as PHP developer for past 6 year.

Friday, August 12, 2011

Find the records which contains non-numeric characters in a field

         To find the records which are all having non-numeric character in a given field using Regular Expression,



select 
empid 
from 
nmc_workflow 
where 
empid ~ '[^0-9]'



Tuesday, July 26, 2011

Search for a pattern in Linux editors like gPHPedit ...

  • To find out a pattern that should start with 'ICON=' and following with alphanumeric words and end with `"`.
 ICON=[\W+\w+]+"
  •  To find out a patter that should start with `" >` and followed by one or more `#` characters
  " >[\s#]+
  •  To find out a pattern that should start with `<DT>` and followed with one or more alphanumeric words
 <DD>[\W+\w+]+


Monday, July 4, 2011

Regular Expressions on OOo Calc


  • Most characters match themselves. There are exceptions - see below.
    COUNTIF(A1:A100; "foo") will return all the instances of the string "foo" in the specified range.
  • "." matches any single character.
    COUNTIF(B2:B18; "..") counts all cells with exactly two characters.
  • "*" is a special character that matches zero or more occurences of the previous expression.
    COUNTIF(B2:B18; ".*e") counts all cells that end in "e".
  • "+" is a special character that matches one or more occurences of the previous single character.
    COUNTIF(B2:B18; ".+m.+") includes "Tmmy" and "name", but not "my".
  • A string of characters enclosed in square brackets ([]) matches any one character in that string.
    COUNTIF(B2:B18; "[efg].*")
    counts all cells starting with e, f, or g.

Tuesday, June 14, 2011

Login form input sanitizer

     $(document).ready(function(){
        $('#id_sfGuardLoginForm').submit(function () {
            $('#signin_username').val($('#signin_username').val().replace(/[^a-zA-Z0-9]+/gi, ''));    
            var pwdstr = $('#signin_password').val().replace(/[\s]+/gi, '');      
            jQuery.trim(pwdstr);
            $('#signin_password').val(pwdstr);
            if(!$('#signin_username').val() && !$('#signin_password').val() ){return false;}
        });
    });

Making of Alphanumeric input using Regular Exp

In Javascript,

        //replace(/[^a-zA-Z.-]/g,'').toUpperCase().;   //Removes other than the list of inputs
        //replace(/^(-)[A-Z.-]+/g,'$1');  //Only allows single -
        //replace(/^[.]/g,'');    //Don't allow . as a first character
        //replace(/^([A-Z.]+)[-]+/g,'$1');    //Remove all the - inbetween the string
        //replace(/[.]+/g,'.');   //Removes more than one occurrences of .
       
        obj.value = obj.value.replace(/[^a-zA-Z.-]/g,'').replace(/^(-)[A-Z.-]+/g,'$1').replace(/^[.]/g,'').replace(/^([A-Z.]+)[-]+/g,'$1').replace(/[.]+/g,'.');


Regular expressions

Check the string to pattern like MSB/2006/00001

        if (subject.match(/^MSB\/\d{4}\/\d{6}$/)) {}



Managing key board events

        $(document).ready(function(){
          
            $('.alphanum').keyup(function () {
                this.value = this.value.replace(/[^a-zA-Z\s0-9:;,.-]/g,'');
            });
            $('.alphanum').blur(function () {
                this.value = this.value.replace(/[^a-zA-Z\s0-9:;,.-]/g,''); 
            });
            $('.alphanum-spl').keyup(function () {
                this.value = this.value.replace(/[^A-Z0-9]/g,'');
            });
            $('.alphanum-spl').blur(function () {
                this.value = this.value.replace(/[^A-Z0-9]/g,''); 
            });
         
            $('.address').keyup(function () {
                this.value = this.value.replace(/[^a-zA-Z\s0-9,-@._]/g,'').toUpperCase();
            });
            $('.address').blur(function () {
                this.value = this.value.replace(/[^a-zA-Z\s0-9,-@._]/g,'').toUpperCase();
            });
            $('.ddo_suff').keyup(function () {
                this.value = this.value.replace(/[^a-zA-Z0-9]/g,'').toUpperCase();
            });
            $('.ddo_suff').blur(function () {
                this.value = this.value.replace(/[^a-zA-Z0-9]/g,'').toUpperCase();
            });
          
       
        });

Tuesday, July 20, 2010

Regular expression to filter special characters and prefixed numbers from a string

            $search_pattern = array ( "/^(-|\s|_|[0-9])+/", 
                                                 "/(-|\s|_|\.|\&)+/e",
                                                 "/(\w+)_(MP3|mp3)+/",);
            $replace = array ("", "_", "$1.$2");


            $string = "- 10_unnaithane.  ---  than...jam____1099_SPB1 & Janaki.mp3";

            echo preg_replace($search_pattern, $replace, $string);

Output :

            unnaithane_than_jam_1099_SPB1_Janaki.mp3





Ref : http://www.webcheatsheet.com/php/regular_expressions.php