|
|
|
Regular expression Fundamentals.
A regular expression (regex) is a powerful way of specifying a complex search. This
article will take you through using them in MySQL.
In MySQL ..
{n} is "repeat-n-times" operator.
. matches any single character.
[...] macthes any character within the brackets.
[abc] matches a, b or c. For a range use [a-z] to match any char and [0-9] any digit.
* 0 or more quantifier, eg.
y* would match any number of y characters.
[0-9]* would match any no of digits.
.* would match any number of anything.
^ assert start of subject and $ assert end of subject.
To make regular experssions work in MySQL use them with REGEXP.
mysql> select * from productinfo where pr_name regexp "^a";
+-------+--------------------+----------+------------+-------------+
| pr_id | pr_name | pr_batch | pr_exp | is_it_suppl |
+-------+--------------------+----------+------------+-------------+
| 1 | Ampoxin 250 mg inj | A00122 | 2004-02-12 | n |
| 2 | Ampoxin 500 mg inj | A0024 | 2004-08-12 | n |
| 3 | Ampoxin 1gm inj | Adf122 | 2005-02-12 | n |
+-------+--------------------+----------+------------+-------------+
3 rows in set (0.00 sec)
To force to a REGEXP comparision to case sensitive use BINARY keyword.
mysql> select * from productinfo where pr_name regexp binary "^a";
Empty set (0.01 sec)
To find batch no containing exacly six chars :
mysql> select * from productinfo where pr_batch regexp "^......$";
+-------+--------------------+----------+------------+-------------+
| pr_id | pr_name | pr_batch | pr_exp | is_it_suppl |
+-------+--------------------+----------+------------+-------------+
| 1 | Ampoxin 250 mg inj | A00122 | 2004-02-12 | n |
| 3 | Ampoxin 1gm inj | Adf122 | 2005-02-12 | n |
+-------+--------------------+----------+------------+-------------+
2 rows in set (0.00 sec)
Above query can be re-written as
mysql> select * from productinfo where pr_batch regexp "^.{6}$";
+-------+--------------------+----------+------------+-------------+
| pr_id | pr_name | pr_batch | pr_exp | is_it_suppl |
+-------+--------------------+----------+------------+-------------+
| 1 | Ampoxin 250 mg inj | A00122 | 2004-02-12 | n |
| 3 | Ampoxin 1gm inj | Adf122 | 2005-02-12 | n |
+-------+--------------------+----------+------------+-------------+
2 rows in set (0.00 sec)
|
|
| This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | MySQL wrapper class (PHP 5+ only) Categories : MySQL, Databases, Classes and Objects, Object Oriented | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | |
|
|
|