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: -------- &...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| 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. |
| |||
| 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." |
| |||
| 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; |
| |||
| 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. |
| |||
| hi, we can validate email address using regular expression as follows PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 ![]() |
| |||
| 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 ![]() |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |