IT Community - Software Programming, Web Development and Technical Support

Php Basic Syntax of Regular Expressions

This is a discussion on Php Basic Syntax of Regular Expressions within the PHP Programming forums, part of the Web Development category; Php Basic Syntax of Regular Expressions -------------------------------------- First of all, let's take a look at two special symbols: '^' and '$'. example: -------- &...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-05-2008, 01:48 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Php Basic Syntax of Regular Expressions

Php Basic Syntax of Regular Expressions
--------------------------------------
First of all, let's take a look at two special symbols: '^' and '$'.

example:
--------

"^The": matches any string that starts with "The";
"of despair$": matches a string that ends in the substring "of despair";
"^abc$": a string that starts and ends with "abc" -- that could only be "abc" itself!
"notice": a string that has the text "notice" in it.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 02:10 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

the symbols '*', '+', and '?', which denote the number of times a character or a sequence of
characters may occur. What they mean is: "zero or more", "one or more", and "zero or one."

Here are some examples:
------------------------

"ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
"ab+": same, but there's at least one b ("ab", "abbb", etc.);
"ab?": there might be a b or not;
"a?b+$": a possible a followed by one or more b's ending a string.

"ab{2}": matches a string that has an a followed by exactly two b's ("abb");
"ab{2,}": there are at least two b's ("abb", "abbbb", etc.);
"ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb").
Note that you must always specify the first number of a range (i.e, "{0,2}", not "{,2}"). Also, as you might
have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1}",
respectively.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 02:12 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

inside parentheses example:
---------------------------

"a(bc)*": matches a string that has an a followed by zero or more copies of the sequence "bc";

"a(bc){1,5}": one through five copies of "bc."
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-05-2008, 02:13 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

The '|' symbol, which works as an OR operator:

"hi|hello": matches a string that has either "hi" or "hello" in it;

"(b|cd)ef": a string that has either "bef" or "cdef";

"(a|b)*c": a string that has a sequence of alternating a's and b's ending in a c;
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-05-2008, 02:14 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

A period ('.') stands for any single character:

"a.[0-9]": matches a string that has an a followed by one character and a digit;

"^.{3}$": a string with exactly 3 characters.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-05-2008, 02:15 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

Bracket expressions specify which characters are allowed in a single position of a string:

"[ab]": matches a string that has either an a or a b (that's the same as "a|b");

"[a-d]": a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and even "[abcd]");

"^[a-zA-Z]": a string that starts with a letter;

"[0-9]%": a string that has a single digit before a percent sign;

",[a-zA-Z0-9]$": a string that ends in a comma followed by an alphanumeric character.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-05-2008, 05:59 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Php Basic Syntax of Regular Expressions

hi,
we can validate email address using regular expression as follows

PHP Code:
if(!eregi("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$"$email))
echo 
"Please Enter valid email"
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-05-2008, 07:05 AM
rajkumar rajkumar is offline
D-Web Trainee
 
Join Date: Nov 2007
Posts: 31
rajkumar is on a distinguished road
Default Php Basic Syntax of Regular Expressions

preg_match
(PHP 4, PHP 5)

preg_match — Perform a regular expression match

Description

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

Parameters

pattern-The pattern to search for, as a string.

subject-The input string.

matches-If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

flags-
flags can be the following flag:
PREG_OFFSET_CAPTURE- If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at index 0 and its string offset into subject at index 1.

offset

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).


Example#1 Getting the domain name out of a URL

<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.discussweb.com/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>



Example#2 Find the string of text "php"

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>



Thanks
Rajkumar


Reference:PHP: Hypertext Preprocessor
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-05-2008, 07:11 AM
rajkumar rajkumar is offline
D-Web Trainee
 
Join Date: Nov 2007
Posts: 31
rajkumar is on a distinguished road
Default Php Basic Syntax of Regular Expressions

Extracting Parts of a String
ereg() and eregi() have a feature that allows us to extract matches of patterns from strings (read the manual for details on how to use that). For instance, say we want do get the filename from a path/URL string -- this code would be all we need:

ereg("([^\\/]*)$", $pathOrUrl, $regs);
echo $regs[1];

Advanced Replacing
ereg_replace() and eregi_replace() are also very useful: suppose we want to separate all words in a string by commas:
ereg_replace("[ \n\r\t]+", ",", trim($str));


Thanks
S.Rajkumar
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-05-2008, 09:38 PM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: Php Basic Syntax of Regular Expressions

Hi

PHP also supports regular expressions using a Perl-compatible syntax using the PCRE functions. Those functions support non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.

<?php
// Returns true if "abc" is found anywhere in $string.
ereg("abc", $string);

// Returns true if "abc" is found at the beginning of $string.
ereg("^abc", $string);

// Returns true if "abc" is found at the end of $string.
ereg("abc$", $string);

// Returns true if client browser is Netscape 2, 3 or MSIE 3.
eregi("(ozilla.[23]|MSIE.3)", $_SERVER["HTTP_USER_AGENT"]);

// Places three space separated words into $regs[1], $regs[2] and $regs[3].
ereg("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)", $string, $regs);

// Put a <br /> tag at the beginning of $string.
$string = ereg_replace("^", "<br />", $string);

// Put a <br /> tag at the end of $string.
$string = ereg_replace("$", "<br />", $string);

// Get rid of any newline characters in $string.
$string = ereg_replace("\n", "", $string);
?>

Thanks
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 02-05-2008, 09:43 PM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: Php Basic Syntax of Regular Expressions

hi

This functions also used for regular expression

* Pattern Modifiers — Describes possible modifiers in regex patterns
* Pattern Syntax — Describes PCRE regex syntax
* preg_grep — Return array entries that match the pattern
* preg_last_error — Returns the error code of the last PCRE regex execution
* preg_match_all — Perform a global regular expression match
* preg_match — Perform a regular expression match
* preg_quote — Quote regular expression characters
* preg_replace_callback — Perform a regular expression search and replace using a callback
* preg_replace — Perform a regular expression search and replace
* preg_split — Split string by a regular expression

Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-06-2008, 05:41 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Php Basic Syntax of Regular Expressions

Hi,
How do we validate a string having number using regular expression?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-08-2008, 08:29 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Php Basic Syntax of Regular Expressions

escape the characters "^.[$()|*+?{\" with a backslash ('\'), as
they have special meaning.

Example 1. Examples of valid patterns

* /<\/\w+>/

* |(\d{3})-\d+|Sm

* /^(?i)php[34]/

* {^\s+(\s+)?$}

Example 2. Examples of invalid patterns

* /href='(.*)' - missing ending delimiter

* /\w+\s*\w+/J - unknown modifier 'J'

* 1-\d3-\d3-\d4| - missing starting delimiter
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How is JavaScript syntax like C / C++? itbarota HTML, CSS and Javascript Coding Techniques 1 09-13-2007 04:09 AM
Regular Expressions simplesabita Testing Tools 1 08-22-2007 04:37 AM
What is the syntax for TextOut function in VC++ Win32 application? mobilegeek C and C++ Programming 3 08-09-2007 03:23 AM
Oracle Cluster - Create and Alter Basic Syntax Murali Database Support 2 07-11-2007 06:40 AM
Python resemble in its class syntax with other languages vigneshgets Python 1 05-27-2007 11:01 PM


All times are GMT -7. The time now is 02:45 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0