Monday, September 27, 2010

Perl Regular Expressions

Regular expression to match only for any type of symbols.

I'm going to write a simple regex that will validate whether the provided string contains only symbols or not. No alphabet or numbers or spaces are allowed. Giving the expression using Perl, but it will work with any programming language as long as it PCRE.


my $str = "^%^";
if( $str =~ /^[!-\/:-@\[-`\{-~]+$/ ){
print "String contains only symbols.\n";
}



So the actual regex is ^[!-\/:-@\[-`\{-~]+$
Posted by Demon at 1:07 AM 0 comments Links to this post
Labels: perl, php
Thursday, December 3, 2009
Regular expression to validate clock time

To validate time from 00:00 to 11:59 you can use a simple regex.

The regex is (?:0[0-9]|1[0-1]):[0-5][0-9]
It means if the first digit of hour is 0, then second digit can be any one from 0-9.
Otherwise if the first digit is 1, that case second digit can only be 1 or 2.

For minute section, first digit can be only from 0-5, and second digit can be from 0-9.

I'm giving you the example using a perl code, but the regex will work with any PCRE compatible engine.

Using Perl


my $time = "01:19";
if($time =~ /(?:0[0-9]|1[0-1]):[0-5][0-9]/){
print "the time is valid\n";
}
else{
print "the time is not valid\n";
}



Simple task, but yet too hard if you don't this :-)
Posted by Demon at 11:25 AM 1 comments Links to this post
Labels: perl, php
Friday, November 13, 2009
Perl: What is the equivalent of Ceil()

Question: At Perl, what is the equivalent function of Ceil? Or how can I perform Ceil on a number like other languages.

Answer: So far, I don't know any function to serve this issue. But if I need, I use sprintf() combined with floating. Below example will show you what exactly I mean for.


my $number = 10.51;
$number = sprintf("%.0f", $number);
print $number;


The above code will output 11, which is the exact ceiling of 10.51.
Posted by Demon at 7:23 AM 2 comments Links to this post
Labels: perl
Perl: What is the equivalent to floor()

Question: What is the equivalent of function floor() at Perl?

Answer: Simple answer is to use int() function. This will just remove the decimal portion from the provided number. Example code using int() to serve like floor is,

print int(10.9);

Posted by Demon at 7:18 AM 0 comments Links to this post
Labels: perl
Wednesday, August 5, 2009
Sql Injection and how to prevent this.

Its one of the dangerous hole to your site which will allow any unwanted guest access your database or control panel. Last few weeks I was working with SQL injecting, and found lots of websites from the worldwide which are welcoming the hackers to use their unknown hole of website.

I'm not that much expert on website coding. You can say I don't work with database. So I don't know much about this stuff. But one thing I learned is, just validate every singe GET or POST parameter from your website script.

Don't trust anyone. Just validate, and check whether its having the format you need or not. If you don't know how to validate your GET/POST parameters, not to worry. Just replace any single quote(') with anything. Don't let this shit to pass.

Or you can use adding slashes types of function depending upon language, to get rid of it. At least it will save your ass for few. But there might be many other ways to find hole. I only know single quoting.

Once again, try to go for validation, even for numbers.
Posted by Demon at 12:23 PM 0 comments Links to this post
Labels: C/C++, java, javascript, perl, php, python, ruby
Perl: Swap string value using s///

This a funny way to swap two values from a string using perl's s///. This will only work if the two words contain exactly one times inside string. I was just wondering about it, and think to add here for all.

Let say you have a string, and you want to swap the words "demo" and "test" with each other. So the simple code will be as below.

## initial string to swap
my $string = "test string and demo string";
## swaping test with demo
$string =~ s/(test)(.*?)(demo)/$3$2$1/;
print "$string\n";



Inside the regex you'll need to change the position of test and demo depending upon the position from original string. And remember this will work only if both string exist only once. For multiple I didn't check, So don't know.

Once again, its a funny stuff, but may come helpful for somebody. Don't forget to add a comment if anyone find such.
Posted by Demon at 11:10 AM 0 comments Links to this post
Labels: perl
Sunday, August 2, 2009
Perl: Various ways to read a file

There is only one way to open or close a file usine open() close() function. You'll find lots of cpan modules which will help you to read file, but internally all the modules are using these to functions to open and close files. I'm showing you how can you read a file into array, or string, or anything else. Just few coding helps regarding file operation.

1) Read the whole file content into an array directly.

open(INFILE, "my @lines = ; ## read into array
close(INFILE); ## close handler



This code will read all the contents from input.txt, and will load into array @lines directly. Later on, you can do whatever operation you need. You have the file on your hand.

2) Read a single line from your input file.

open(INFILE, "my $line = ; ## read a single line
close(INFILE); ## close handler


Perl interpreter is intelligent enough to understand your need. Since you provided a scalar to file handler, it will return you the current line that the file handler pointing. This case it will return you only the first line.

3) Using a loop, say while(), process each line while reading without using any array.

open(INFILE, "while(){
# current line at the variable $_
## process here
}
close(INFILE); ## close handler



If you don't want to use the default $_ variable, that case you can use this loop.

while (my $line = ){
print $line;
}



4) Read the whole file content into a string instead of array. I used to use join() function to do this.

open(INFILE, "my $file_content = join('', );
close(INFILE); ## close handler



This join function just take the whole file as array, and join each line from the array. A bit tricky, but nothing else.

I think this will cover what you need when you'll do some file operation at perl scripting.
Posted by Demon at 12:35 AM 0 comments Links to this post
Labels: perl
Friday, July 24, 2009
Perl: Explanation of your regular expression

Perl CPAN has a module called "YAPE::Regex::Explain" which will help you to understand what is a perl regular expression doing.

Lets say you have a Regex [Za-z0-9]+ Here is the sample code if you want to get the explanation of your regex.

use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new('[A-Za-z0-9]+')->explain;


This simple code will output as below on your screen, and which is easily understandable to get what is going on.

The regular expression:

(?-imsx:[A-Za-z0-9]+)

matches as follows:

NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
[A-Za-z0-9]+ any character of: 'A' to 'Z', 'a' to 'z',
'0' to '9' (1 or more times (matching the
most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------




Lets try with a bit complex regular expression ST[^;]*(?:[A-Za-z0-9]+|[\.,\s]+)ET

The code for explaining for REGEX will be,

use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new('ST[^;]*(?:[A-Za-z0-9]+|[\.,\s]+)ET')->explain;

Output is as below, and which is damn understandable.

The regular expression:

(?-imsx:ST[^;]*(?:[A-Za-z0-9]+|[\.,\s]+)ET)

matches as follows:

NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
ST 'ST'
----------------------------------------------------------------------
[^;]* any character except: ';' (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[A-Za-z0-9]+ any character of: 'A' to 'Z', 'a' to
'z', '0' to '9' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[\.,\s]+ any character of: '\.', ',', whitespace
(\n, \r, \t, \f, and " ") (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
ET 'ET'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------



Running the code from Command Line:

If you don't like to write a code for this, you can do it from your command line. Just one liner perl script. For above expression the code will be as below.

perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new('ST[^;]*(?:[A-Za-z0-9]+|[\.,\s]+)ET')->explain;"

How to install the module:

To install the module from your command prompt,
ppm install YAPE::Regex::Explain

And then type
ppm install YAPE::Regex

This installation can be done using cpan too.
Posted by Demon at 6:39 AM 2 comments Links to this post
Labels: perl
Friday, July 10, 2009
Perl: Http::Cookies not saving cookie correctly

The below code for saving the http cookie after requesting some data over http is not working!!! Did you face this kind of problem anyway?

$cookie_jar->save()


For example the server sent you 4 cookies. But your cookie_jar is only saving the 2 of them, and rest of the two are ignored. Do you know the reason behind this?

It's not a bug of http cookies of perl, or lwp. Just lack of knowing the issue. Initially I thought it was a bug. The rest of the two cookies are session cookie. And it will die when you'll close your browser( this case your LWP ). That's why Http::Cookies is not saving the cookie on your file. Here is what it was said on the docs of HTTP::Cookies,

$cookie_jar->save
$cookie_jar->save( $file )

This method file saves the state of the $cookie_jar to a file. The state can then be restored later using the load() method. If a filename is not specified we will use the name specified during construction. If the attribute ignore_discard is set, then we will even save cookies that are marked to be discarded.

The default is to save a sequence of "Set-Cookie3" lines. "Set-Cookie3" is a proprietary LWP format, not known to be compatible with any browser. The HTTP::Cookies::Netscape sub-class can be used to save in a format compatible with Netscape.

HTTP::Cookies

So, this means you have to notify the cookie_jar to save your session cookie too. To do this you have to do as below.

$cookie_jar = HTTP::Cookies->new(
file => "cookie.txt",
ignore_discard => 1,
);

$cookie_jar = HTTP::Cookies->new(
ignore_discard => 1,
);

Posted by Demon at 11:38 PM 2 comments Links to this post
Labels: perl
Saturday, June 27, 2009
Perl: Left padding an integer using sprintf

Using the function sprintf() of Perl, you can easily left pad any given integer number easily. The sprintf() function is similar to use like other programming languages. I'm giving an example of showing a given integer number with 5 field, where left sides will filled with zeros if any.


my $integer = 10;
$integer = sprintf("%05d", $integer);
print "$integer\n";


Just a simple left padding.
Posted by Demon at 10:05 AM 0 comments Links to this post
Labels: perl
Thursday, June 18, 2009
Perl: how to get position of a certain character in a string

There is a function called strpos() at php for this, and if you are looking for the similar function support at perl, then you have index() at Perl.

The index() function takes string and search string as parameter. Here is the example code.

$string = "tester of the world";
$search_string = "r";
print index($string, $search_string);



This will output 5, as 5 is the first occurrence or search string from the provided string. You can use character, or string as search. And remember the index starts from 0. At above example r is at the 5th index.
Posted by Demon at 12:00 PM 0 comments Links to this post
Labels: perl
Tuesday, June 9, 2009
Perl Number formating using Regexp

Using a single regex you can easily convert a number like 1234567890 into 1,234,567,890. I don't what this call, that's why I was put the title is number formatting. Here goes the regular expression with example.

my $number = 1234567890;
$number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
print $number;

Output : 1,234,567,890



The above regex just takes the digits from last, or three digits just before the comma, and if another extra digit exist without those three, it just push a comma inside. This will make one digit goes to left side, and other three digits goes to right side.

Just a simple regexp made your life easier to do such kind of conversion.
Posted by Demon at 10:59 AM 0 comments Links to this post
Labels: perl
Sunday, May 31, 2009
Remove the words having special characters from a string

Lets learn how to remove the words from a string which contains the special characters. I mean the word having non-English characters. Consider the below example.
asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8

You can see several words from the above contains special/non-english character on them. I'll write a regex that will remove those kind of words from the sentence above.

A PHP code:

$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string = preg_replace('/\S*[^a-z0-9A-Z\s,\.]+\S*/', '', $string);
print "$string\n";


Output: asdasd 123, asda koskso

A Perl code:

$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string =~ s/\S*[^a-z0-9A-Z\s,\.]+\S*//g;
print "$string\n";


Output: asdasd 123, asda koskso

You can use trim on the string, or replace multiple spaces with single one after that. Also you can add other characters like dot, comma inside the regex to distinguish the word.

Cheers!!!
Posted by Demon at 9:24 AM 1 comments Links to this post
Labels: perl, php
Saturday, May 30, 2009
Conditional Log filtering for your apache

At my website I found that there were lots of log entry at my access_log for GIF images. I wanted to find a way so that there will be no entry at my access_log for GIF images. I found the way, and here is how you need to filter your log entry for GIF images.

SetEnvIf Request_URI \.GIF$ no-log
CustomLog log/access.log combined env =! no-log

This will help you to prevent writing any entry at your log file. If you want to add more file types at your filtering condition, you can use them using pipe(|). For example below.

SetEnvIf Request_URI "\.(PNG|png|gif|GIF)$" no-log
CustomLog log/access.log combined env =! no-log

But for anything, make sure that you have setenvif module is loaded. To check this you can use either httpd -M or apache2 -M depending upon your system.

Hope this little information will help you to setup custom logging for your apache.
Posted by Demon at 7:26 AM 0 comments Links to this post
Labels: perl, php
Thursday, May 28, 2009
Perl : Get Number of elements from hash

How to get the number of elements a Perl hash holding?

Using the keys() function of Perl, you can easily get the count. The function used to return all the keys from the hash as array. But if you use a SCALAR variable, that case it will return you the element count from the hash.

my %hash = (red => 1, blue => 2, green => 3);
my $count = keys(%hash); ## Scalar requested.
print $count;

Is there any other method to get the elements count from a Perl hash? I mean not using a loop.
Posted by Demon at 7:43 AM 0 comments Links to this post
Labels: perl
Wednesday, May 20, 2009
perl: get IP address of your website user

From your Perl/CGI script, you can easily get the remote IP address of the user who is browsing your website. I'm providing a tiny code that will simply print out the IP address of the user.

use CGI; ## load the cgi module

print "Content-type: text/plain; charset=iso-8859-1\n\n";
my $q = new CGI; ## create a CGI object
print $q->remote_host(); ## print the user ip address

The output will be the simply the IP address of the user who will browse the page. You'll find more about the CGI module of Perl at http://perldoc.perl.org/CGI.html
Posted by Demon at 10:22 AM 1 comments Links to this post
Labels: perl
Tuesday, May 5, 2009
Perl: print last element from array

We know an array needs to access with positive integer. But scripting language like Perl, you can access it using Negative number. A negative number means from last. If you use -5, it will give you the 5th element from the array from last. Remember, for negative index, the index start from 1. Means -1 = last index, -2 = second last, and so on.

This example will help you to understand much more clearly.

## initial array
my @array = qw(1 2 3 4 5 6 7 8 9 10);

## print last element from array
print $array[-1] , "\n";

## print second last element from array
print $array[-2] , "\n";



At above example, there are 10 elements. if you provide a negative number which is greater than the total number of elements, you'll get error Use of uninitialized value in print at temp.pl line 2., for example the below code will generate such error.

print $array[-11] , "\n";


The reason is the array doesn't have 11 elements in total.
Posted by Demon at 8:14 AM 0 comments Links to this post
Labels: perl
Perl: Convert HEX string into character string

At my last post http://icfun.blogspot.com/2009/05/perl-convert-character-string-into-hex.html I've mentioned about character string conversion into HEX string.

Now, its time to do the reverse task. You'll be given a Hex string, and you'll need to convert it into character string. The idea will be similar as last one. Just need to pick-up two hex digit each time, and convert them into character code. Below is the code.

## Initial hex
$string = "6162636465666a68696a6b6c6d6e6f707172737475767778";
## upper/lower case won't be a problem
# $string = "6162636465666A68696A6B6C6D6E6F707172737475767778";

## convert each two digit into char code
$string =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;

print "$string\n";



This time using hex() function I have converted the hex code into integer number, and then using chr() function, I have converted the ascii number into it's character code.
Posted by Demon at 7:46 AM 3 comments Links to this post
Labels: perl
Perl: Convert character string into HEX string

Lets learn about converting a character string into a HEX string. For example you have a string "abcd", it will convert it into HEX string as "61626364". Means just take each character from the string, and get its ascii code, and convert it into HEX.

I'm using regular expression (REGEX) to do this conversion. Below is the code.

## Initial string
$string = "abcdefjhijklmnopqrstuvwx";

## convert each character from the string into HEX code
$string =~ s/(.)/sprintf("%x",ord($1))/eg;

print "$string\n";


The above code will convert the provided string string into it's corresponding HEX code. Using regex i'm picking up the each character from the string, and get the ascii code using ord() function. Then using sprintf() I've just printed as HEX code.

If you want the output as UPPERCASE, you'll just need to make %x into %X inside your sprintf() function.

$string =~ s/(.)/sprintf("%X",ord($1))/eg;



The output will be as below for Lowercase, and Upper case:

6162636465666a68696a6b6c6d6e6f707172737475767778
6162636465666A68696A6B6C6D6E6F707172737475767778

The reverse one is at http://icfun.blogspot.com/2009/05/perl-convert-hex-string-into-character.html
Posted by Demon at 7:38 AM 3 comments Links to this post
Labels: perl
Thursday, April 30, 2009
Get the N-th Bit value of any Integer number

Once again bitwise operations guys. This time we'll know about finding the nth bit value of any 31 bit positive integer number. For example you have a number 120, and you want to get the 7th bit of the number. Here is a function in C/C++ code. But you can convert the code into Perl/Python/Java/PHP/Ruby or whatever you want. The basic is the same.

The bit position are start from left to right.

/**
* Function takes the decimal number
* Function takes the Nth bit (1 to 31)
* Return the value of Nth bit from decimal
*/
int get_bit(int decimal, int N){

// Shifting the 1 for N-1 bits
int constant = 1 << (N-1);

// if the bit is set, return 1
if( decimal & constant ){
return 1;
}

// If the bit is not set, return 0
return 0;
}



The above function will take a 31 bit integer number, and the second parameter is the N(1 to 31), means which position you are interested. Here is few example code to use the function


printf("%d\n", get_bit(3,1));
printf("%d\n", get_bit(12100,7));
printf("%d\n", get_bit(12731273,29));
printf("%d\n", get_bit(2111823716,18));



The output will be:
1
1
0
1

1st bit value of 3 is = 1
7th bit value of 12100 is = 1
29th bit value of 12731273 is = 0
18th bit value of 2111823716 is = 1

Bit-wise operations are fun to use, and faster too. The example is I've given is very basic, and the syntax will work for almost every programming languages. So you can convert the tiny C/C++ code into others. I'll appreciate if anyone convert that into other language, and post as a comment here.
Posted by Demon at 8:37 AM 0 comments Links to this post
Labels: C/C++, java, perl, php, python, ruby

No comments:

Followers