Most basic regular expression consists of list of metacharacters. These metacharacters have a special meaning in regular expression. In this post I will show you the list of eleven metacharacters exist in most regular expression and their meaning.
1)Dot .: Dot matches any single character except a newline. For example, if it is said a.b then it can indicates aab or axb or avb or a%b etc.
But remember that if you use dot (.) within square bracket then it becomes literal. If we use [a.b] then it describes only "a" or "." or "b".
2)Bracket[]: Bracket matches a set of possible character contains inside it. For example if we write [abcde] then it describes "a" or "b" or "c" or "d" or "e". Within bracket we can also specify a range of characters using dash (-). Suppose, [a-z] specifies a range which matches any lowercase letter from "a" to "z". We can use it in various ways. Like [mp-s] indicates "m" or "p" or "q" or "r" or "s", also we can indicate same expression by, [mpq-s].
But remember that if dash(-) is specified as last character inside bracket or specified after backslash then it becomes literals. Suppose [mnq-] expresses "m" or "n" or "q" or "-". [mp\-s] denotes "m" or "p" or "-" or "s".
3)Caret within bracket [^]: Caret within bracket just do negation. If we specify caret inside bracket then it matches all characters that is not inside bracket. For example if we write [^bcd] then it matches any characters other than "b","c","d". If we write [^m-p] then it matches any single character that is not matches any lowercase character between m to p., i.e not m, n, o, p.
4)Caret ^: Caret (^) means it matches beginning of a line or beginning of a string.
5)Dollar sign $: Dollar sign ($) means it matches ending position of a line or ending position of a string.
6)Plus sign +: Plus sign (+) matches the preceding pattern element one or more times. For example if we write, xy+z then it matches xyz, xyyz, xyyyz etc.
7)question mark ?: Question mark (?) matches the preceding pattern element zero or one time.
For example if we write xy?z then it matches only xz and xyz.
8)Asterisk sign *: Asterisk (*) matches the preceding pattern element zero or more times.
For example if we write xy*z then it matches xz, xyz, xyyz etc.
9)vertical bar or pipe symbol |: It matches the expression before vertical bar or matches the expression after vertical bar. If we write a|b then it matches either "a" or "b".
10){M,N}: It expresses the minimum M and the maximum N match count of the preceding character. For example if we write a{M,N} "a" matches minimum M times and maximum N times.
11)\b: Matches a word boundary. For example, \b upon word "test" matches st, t, est etc.
12)\w: Matches an alphanumeric character, including "_". For example, \w matches [a-zA-Z0-9_].
13)\W: Matches a non-alphanumeric character, excluding "_". So, \W matches [^a-zA-Z0-9_].
14)\s: Matches a whitespace character (space, tab, newline, form feed). So, \s matches space, tab, newline, form feed.
15)\S: Matches anything except a whitespace.
16)\d: Matches a digit. We can also donate as [0-9].
19)\D: Matches a non-digit. We can also donate as [^0-9].
Related Documents
http://arjudba.blogspot.com/2009/07/what-is-regular-expression-or-regex-or.html
http://arjudba.blogspot.com/2009/06/how-to-add-word-or-letter-at-end-of.html
http://arjudba.blogspot.com/2009/06/how-to-add-line-to-first-in-file-using.html
Thursday, July 30, 2009
List of metacharacters in regular expression(regexp)
| Reactions: |
What is regular expression or regex or regexp
Regular expression or in other words regex or regexp is a pattern that describes particular characters or words or patterns of characters. Regular expressions provide very flexible way to to express a set of characters that we are interest of.
You may have questions about why regular expression comes? What is the use of it? To answer it, let's look a simple example below.
You have a written large piece of code in your development environment where is used upper version of php and the code contains following words.
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':companyName', $companyName);
There exists this types of statements many times and many places in your code. Suppose you have 40,000 lines of code and out of 40,000 lines you have this type of statements in 5,000 places. Now in your production you see php version there does not support bindparam function. So what you need to do? You need to search all 40,000 lines and find the 5,000 places where bindparam function exists and then replace the function with suitable function that is supported by that version as well as corresponding syntax of that function.
In this example we are needed to change all $stmt->bindParam by mysql_real_escape_string as well as we don't need bind variable and their quote, for example we exactly need to look as
mysql_real_escape_string( $userName);
mysql_real_escape_string( $password);
mysql_real_escape_string( $firstName);
mysql_real_escape_string( $lastName);
mysql_real_escape_string( $companyName);
In this scenario with using regular expression we can save a lots of time just using a single expression wise search and replace command. If you just do search
$stmt-<bindParam(':any_character_here',
and replace them by
mysql_real_escape_string(
then our goal will be achieved. Regular expression does the trick, search for pattern
$stmt-<bindParam('.*',
and then replace them by,
mysql_real_escape_string(
where .* combination denotes regular expression.
-Dot(.) indicates matches any character except a newline.
-* indicates matches the preceding pattern element zero or more times.
So if there is such pattern found like
first comes $stmt-<bindParam('
and finish by ', which is followed by any combination of characters that to be replaced by mysql_real_escape_string(
We see here one command will do all 5,000 searches and replaces instead of searching/typing those.
We also need regular expression to express certain types of patterns while writing something. Like someone wrote , "I love grey|gray color". Here | is regular expression which indicates any of grey or gray.
This is all about regular expression, their usage. In subsequent post I will go more with regular expressions.
Related Documents
How to add a word or letter at the end of each line in shell script
How to add a line to the first in a file using shell script
You may have questions about why regular expression comes? What is the use of it? To answer it, let's look a simple example below.
You have a written large piece of code in your development environment where is used upper version of php and the code contains following words.
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':companyName', $companyName);
There exists this types of statements many times and many places in your code. Suppose you have 40,000 lines of code and out of 40,000 lines you have this type of statements in 5,000 places. Now in your production you see php version there does not support bindparam function. So what you need to do? You need to search all 40,000 lines and find the 5,000 places where bindparam function exists and then replace the function with suitable function that is supported by that version as well as corresponding syntax of that function.
In this example we are needed to change all $stmt->bindParam by mysql_real_escape_string as well as we don't need bind variable and their quote, for example we exactly need to look as
mysql_real_escape_string( $userName);
mysql_real_escape_string( $password);
mysql_real_escape_string( $firstName);
mysql_real_escape_string( $lastName);
mysql_real_escape_string( $companyName);
In this scenario with using regular expression we can save a lots of time just using a single expression wise search and replace command. If you just do search
$stmt-<bindParam(':any_character_here',
and replace them by
mysql_real_escape_string(
then our goal will be achieved. Regular expression does the trick, search for pattern
$stmt-<bindParam('.*',
and then replace them by,
mysql_real_escape_string(
where .* combination denotes regular expression.
-Dot(.) indicates matches any character except a newline.
-* indicates matches the preceding pattern element zero or more times.
So if there is such pattern found like
first comes $stmt-<bindParam('
and finish by ', which is followed by any combination of characters that to be replaced by mysql_real_escape_string(
We see here one command will do all 5,000 searches and replaces instead of searching/typing those.
We also need regular expression to express certain types of patterns while writing something. Like someone wrote , "I love grey|gray color". Here | is regular expression which indicates any of grey or gray.
This is all about regular expression, their usage. In subsequent post I will go more with regular expressions.
Related Documents
How to add a word or letter at the end of each line in shell script
How to add a line to the first in a file using shell script
| Reactions: |
Tuesday, July 28, 2009
A different idea of advertising your site
Do you live in a developing country like Romania , Bangladesh , Pakistan , India or China ? Well, if you also live in a developed country like United States or Canada you might also
choose to live or buy space in other country. You might not have the ability to do so as you cannot afford it. But in the site http://www.buyaplaceonearth.comthere brings an opportunity to buy space of
another place wherever you live in.
The site provides you the possibility to become the owner of any place on earth, and post there a picture/logo so that whole internet users can see it. You can buy any part of your own country, part of any ocean, whole continent or you can choose other continent different that you live. Imagine you have space in an ocean where no man ever reach there.
In the 3d pixel advertising the earth is full 3D and interactive, you can zoom in/out and spin it whatever you prefer. That is you can see in detail level before buying your place on this earth.
Now let’s analyze some advantages of having this space of the earth.
Advantages
1) 3D Format! – something new on web, the technology is at the beginning, but it has great potential, I personally found another 3D Globe just on Discovery Website. I repeat, this is the first 3D Advertising webpage!
1) 3D Format! – something new on web, the technology is at the beginning, but it has great potential, I personally found another 3D Globe just on Discovery Website. I repeat, this is the first 3D Advertising webpage!
2) It intrigues the visitor’s curiosity way better than a plane pixel advertising website where the positioning doesn’t mean anything! Also it more efficient than a banner on any other website because there is no information/content that can distract the visitor’s attention from the ads.
3) Extremely low costs, considering that the images will remain there for at least 5 years, and the payment is made only once (not each month nor year) .It is a future investment for those who want to make their company better known, or for those who just want to bring more visitors to their website. Note that all visitors brought to your website are UNIQUE.
4) It can work as a web directory but in a different and new format.
5) In stead of traditional advertising, you can have now a very good look and feel advertisement.
6) You can also focus a particular geography by buying space for that geography as well as you can focus entire world.
List of Available Advertising Network Companies
| Reactions: |
Sunday, July 26, 2009
How to retrieve checkbox value from mysql database
In this post I will show you how to retrieve checkbox value from database. As in the database in one field there resides multiple checkbox value so we need to be cautious while displaying checkbox data specially which box to be checked and which not.
Let's assume that field values for method #1 that is described inside http://arjudba.blogspot.com/2009/07/entering-mutiple-values-with-checkbox.html
We assume that checkbox field data are inserted into the database using comma separated values.
BY manually we are doing here for testing purpose.
mysql> delete from checkbox;
Query OK, 2 rows affected (0.00 sec)
mysql> insert into checkbox values('meat,vegetable,fastfood');
Query OK, 1 row affected (0.00 sec)
Now following is the php code that is used to display for checkbox. Specially look at the condition,
Whenever you execute above php code output like below will be displayed in the browser.
Related Documents
http://arjudba.blogspot.com/2009/07/entering-mutiple-values-with-checkbox.html
Let's assume that field values for method #1 that is described inside http://arjudba.blogspot.com/2009/07/entering-mutiple-values-with-checkbox.html
We assume that checkbox field data are inserted into the database using comma separated values.
BY manually we are doing here for testing purpose.
mysql> delete from checkbox;
Query OK, 2 rows affected (0.00 sec)
mysql> insert into checkbox values('meat,vegetable,fastfood');
Query OK, 1 row affected (0.00 sec)
mysql> select * from checkbox;
+-------------------------+
| foods |
+-------------------------+
| meat,vegetable,fastfood |
+-------------------------+
1 row in set (0.00 sec)
Now following is the php code that is used to display for checkbox. Specially look at the condition,
if (in_array($food,$foods_ex)){
echo "<td>";
echo "<input type=\"checkbox\" name=\"food_cat[]\" value=$food checked>$food</input>";
echo "</td>";
}
else{
echo "<td>";
echo "<input type=\"checkbox\" name=\"food_cat[]\" value=$food>$food</input>";
echo "</td>";
}
<?
include('include/db.php');
$query="select foods from checkbox limit 1";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$row = mysql_fetch_array($result);
extract($row);
}
mysql_close();
?>
<html>
<body>
<form method="post">
<table width="50%">
<th>What is your favorite food?</th>
<tr>
<?
$array_food=array('fish','meat','vegetable','fastfood','soup');
$foods_ex=explode(',',$foods);
//print_r ($foods_ex);
foreach($array_food as $food){
if (in_array($food,$foods_ex)){
echo "<td>";
echo "<input type=\"checkbox\" name=\"food_cat[]\" value=$food checked>$food</input>";
echo "</td>";
}
else{
echo "<td>";
echo "<input type=\"checkbox\" name=\"food_cat[]\" value=$food>$food</input>";
echo "</td>";
}
}
?>
</tr>
<tr>
<td colspan="6" style="padding-left:30em">
<input type="submit" name="submit" value="save">
</td>
</table>
</form>
</body>
</html>
Whenever you execute above php code output like below will be displayed in the browser.
Related Documents
http://arjudba.blogspot.com/2009/07/entering-mutiple-values-with-checkbox.html
| Reactions: |
Entering multiple values with checkbox into mysql
In this post I will discuss how I can pass multiple values of checkbox into mysql database. If we try to insert checkbox data in normal way then we will see only one value of it is inserted. Also, there is exactly one field to hold multiple checkboxes data unless you want to insert multiple records into database. Now the question is how to insert multiple values into one field for checkbox data.
There are several ways by which we can insert multiple checkbox data into mysql database. In the following section it is discussed.
Way 01 Using Implode function
1)Prepare table to hold multiple checkbox data.
Declare as varchar of the field that will hold checkbox data. Note that your database field must be greater than the summation of all checkbox values length so that it can hold multiple checkbox value.
mysql> create table checkbox(foods varchar(60));
Query OK, 0 rows affected (0.14 sec)
mysql> select * from checkbox;
Empty set (0.00 sec)
2)Note that to insert multiple checkbox values, all checkbox html name must be declared as array. Like in this example it is declared as,
-<input type="checkbox" name="all_food[]" value=$food>$food</input>
And while inserting value into database we need to append a character like comma(,) after each checkbox value so that we can distinguish multiple checkboxes data. This is done by implode function as in described in the example.
If we run above php code and after check different checkboxes multiple values are inserted into database as below.
Way 02: Inserting data using serialize function
From the database it will display,
Here,
- a:3 indicates how many checkbox values are there.
- i:0 indicates it is the first checkbox value inserted. i:1 means it is in the serial second to be inserted data into mysql database and so on.
- s:4 indicates how many letters in the value. s:4 means four letters, s:8 means eight letters.
Related Documents
Datatypes in Php
Integer and floating point number in Php
Variables in PHP
There are several ways by which we can insert multiple checkbox data into mysql database. In the following section it is discussed.
Way 01 Using Implode function
1)Prepare table to hold multiple checkbox data.
Declare as varchar of the field that will hold checkbox data. Note that your database field must be greater than the summation of all checkbox values length so that it can hold multiple checkbox value.
mysql> create table checkbox(foods varchar(60));
Query OK, 0 rows affected (0.14 sec)
mysql> select * from checkbox;
Empty set (0.00 sec)
2)Note that to insert multiple checkbox values, all checkbox html name must be declared as array. Like in this example it is declared as,
-<input type="checkbox" name="all_food[]" value=$food>$food</input>
And while inserting value into database we need to append a character like comma(,) after each checkbox value so that we can distinguish multiple checkboxes data. This is done by implode function as in described in the example.
<?
if(isset($_POST['submit'])) {
include('include/db.php');
$arrays = implode(",",$_POST['all_food']);
$query="insert into checkbox values('".$arrays."')";
//echo $query;
mysql_query($query) or die ('Error inserting values into database, please go back and try again');
mysql_close();
}
?>
<html>
<body>
<form method="post">
<table width="50%">
<th>What is your favorite food?</th>
<tr>
<?
$array_food=array('fish','meat','vegetable','fastfood','soup');
foreach($array_food as $food){
echo "<td>";
echo "<input type=\"checkbox\" name=\"all_food[]\" id=\"all_food\" value=$food>$food</input>";
echo "</td>";
}
?>
</tr>
<tr>
<td colspan="6" style="padding-left:30em">
<input type="submit" name="submit" value="save">
</td>
</table>
</form>
</body>
</html>
If we run above php code and after check different checkboxes multiple values are inserted into database as below.
mysql> select * from checkbox;
+--------------------------+
| foods |
+--------------------------+
| meat,vegetable,fast,soup |
| meat,vegetable,fastfood |
| fish,meat,fastfood |
+--------------------------+
3 rows in set (0.00 sec)
Way 02: Inserting data using serialize function
<?
if(isset($_POST['submit'])) {
include('include/db.php');
$arrays = serialize($_POST['all_food']);
$query="insert into checkbox values('".$arrays."')";
//echo $query;
mysql_query($query) or die (mysql_error());
mysql_close();
}
?>
<html>
<body>
<form method="post">
<table width="50%">
<th>What is your favorite food?</th>
<tr>
<?
$array_food=array('fish','meat','vegetable','fastfood','soup');
foreach($array_food as $food){
echo "<td>";
echo "<input type=\"checkbox\" name=\"all_food[]\" value=$food>$food</input>";
echo "</td>";
}
?>
</tr>
<tr>
<td colspan="6" style="padding-left:30em">
<input type="submit" name="submit" value="save">
</td>
</table>
</form>
</body>
</html>
From the database it will display,
mysql> select * from checkbox;
+--------------------------------------------------------------+
| foods |
+--------------------------------------------------------------+
| a:3:{i:0;s:4:"meat";i:1;s:9:"vegetable";i:2;s:8:"fastfood";} |
| a:3:{i:0;s:4:"fish";i:1;s:4:"meat";i:2;s:8:"fastfood";} |
| a:2:{i:0;s:4:"meat";i:1;s:4:"soup";} |
+--------------------------------------------------------------+
3 rows in set (0.00 sec)
Here,
- a:3 indicates how many checkbox values are there.
- i:0 indicates it is the first checkbox value inserted. i:1 means it is in the serial second to be inserted data into mysql database and so on.
- s:4 indicates how many letters in the value. s:4 means four letters, s:8 means eight letters.
Related Documents
Datatypes in Php
Integer and floating point number in Php
Variables in PHP
| Reactions: |
Subscribe to:
Posts (Atom)
Tag Cloud
10.2g
10g
11g
11gR2
Abasa
About Oracle
Administration
Adsense
Alerts
Archival
ASM
ASP.Net
Audit
Audit Vault
Backup
Bangladesh
Block Corruption
Blogger
Browser
Bug
Business
Clone
Clusterware
Comments
Concepts
Connection
Controlfiles
Crime
CSS
Data Block
Data Dictionary
Data Guard
Data Mining
Data Pump
Data Type
Database Administration
Database Vault
DBConsole
Developer
Economics
EM
Excel
Exercise
Explain plan
Export
External Table
Facebook
Firefox
Firmware
Flashback
Forum
Functions
Games
Globalization Support
Grid Control
Hardware
History
HTML
IE
Import
Indexes
initializaion parameter
initialization parameter
Installation
Internals
Internet
Interview
isql*plus
Java
JavaScript
Job
Joins
Joke
Limitation
Linux
Listener
Logminer
Magento
Mail
Materialized View
Medical
Memory
Mobile
Money
Multimedia
MySQL
Net Services
Network
OCP
Operators
Oracle
Oracle Concepts
Oracle Recovery
OS
Others
OUI
Package
Packages
Parameters
Partitioning
Patchset
Performance
Perl
Pfile
Photos
PHP
PL/SQL
Profile
Pseudocolumns
Puzzle
Quiz
Quota
RAC
RAC Installation
Recovery
Recovery Problems
Redo Log
Reports
RMAN
Scripts
Security
SEO
Server Administration
SGA
Shell Script
Smarty
Social Marketing
Solaris
Spfile
SQL
SQL Tuning
SQL*Loader
Sql*Plus
Startup Problem
Streams
SwingBench
System Analysis
Tablespaces
Technology
Temp
TNS Error
Tools
Troubleshooting
Tuning
Undo
UNIX
Upgradation
Utilities
Version
Views
Vmware
Windows
Wordpress
XML