Jump to content
xisto Community
Sign in to follow this  
mahesh2k

Regular Expression

Recommended Posts

I know that regex are language agnostic because of the wide areas where it can be used. I have used regeex in ruby on rails, python and now in php. I have found it very handy. I think like string operations they are also fast. Most of the string operations and regex are common. You just need regex in araes where string cant do the job. For example, in areas where yuo want to do the seach and replace operations, regex is very useful. I know that some think regex is very slow but I have seen no performance change with the regex. Maybe it could be for large programs or for server based operations. So far nothing observable from my end.

I have tried the python regex at first because I found that using python first to experiment it as python requires less code. So I tried with that and then later on moved to php regex. I am going to use the stuff in some of my php based stuff. I don't know how much I have learned regex perfectly but if they are going to save me time then by all means i am going to use them in my programs. I also see the use of the regex in file maangement and some of the other stuff related to the i/o. I have not done much of that in php but would love to do that.

Currently I am reading some of the posts from the google programmer R.cox about the regex. As his regex knowledge was used in the google code search. I see that his posts do make sense about the usage and speed of regex. I know that i am not going to use the regex for anything complex like that. I am just testing the waters of regex as of now. I will post more based on my observation with regex. I would love to know if anyone else is using regex in their works and have any good suggestions, books and posts to watch for.

Share this post


Link to post
Share on other sites

I am using regex whenever possible in PHP with preg_match and preg_replace functions to test things depending on a pattern. In university I had a class for one semester which included regular expressions, but it had more theory than practice, but at least I learned the purpose of it and where it's widely used, the technology itself is really powerful and PERL strings with regular expressions as I know is one of the best implementation.

Anyway whenever you need to test user data or play with strings, usually regular expressions saves a lot of coding rather than using a lot of string functions, you can check with a regular expression if it's an url, email and a lot more, it really can almost do anything what any programming language can do. Here are some examples:

// Trim text, same as trim($string) I guess;$string = preg_replace('/^\s+|\s+$/u', '', $string );// Trim empty lines, before that normalize newlines to \n$string = preg_replace('/[ ]+\n/u', "\n", preg_replace('/(\r\n)|(\r)/u', "\n", $string ) );// Normalize new lines, replace 3 and more newlines to two newlines$string = preg_replace('/\n{3,}/u', "\n\n", $string );// Normalize spaces, replace 2 and more spaces to one space character$string = preg_replace('/[ ]{2,}/u', ' ', $string );// Check name of somekind, \pL and \u let you use all letters, even chinesse and arabic alhpabets, \i means case insensitiveif ( !preg_match("/^[\da-z \-\.\pL]+$/iu", $_POST['contact_name'] )){echo 'Name like that is not allowed! You can use A-Za-z or any other unicode character, all digits and a space, minus (-) , dot (.)';}// Check emailif ( !preg_match("/^([a-z0-9._-\pL](\+[a-z0-9\pL])*)+@[a-z0-9.-\pL]+\.[a-z\pL]{2,6}$/iu", $email )){echo 'Not a valid email!';}// Check url, can be http or https or ftpif ( !preg_match('@((ht|f)tps?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $url )){echo 'Please enter a valid website address';}

Regular expressions are really powerful, they can be much more complex and you can even use a callback function with the function preg_replace_callback() which lets you on every pattern match execute your custom or existing function for that match found.

The first time I saw regular expressions, it seemed really strange and something I will never learn, but once you get a hang of them, they're very useful, even in Oracle database you can use REGEX as of version 10g as I remember for your SQL statements :)

In addition I can say, that don't overuse regular expressions, even though their powerful, but they are slower, even though if you don't have thousands of requests per second, it usually won't be a problem, but still, use it when necessary. :)

By the way, it's best to write your regular expression on a regular expressions engine which can show you results of the matches, there are plenty of them online, for example:

http://www.pagecolumn.com/tool/pregtest.htm
http://forums.xisto.com/no_longer_exists/

;]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.