Regular expression find String,Word,Numbers in String
Posted by Raj
Regular expression find string,word,numbers in string.
In this section,I will explain how to find words,string,numbers in string using Regular expression.
It is very easy to match strings or patterns of characters in php using regular expresions.
A regular expression provides a matching strings of word,particular characters, numbers,or patterns of characters.Abbreviations for `regular expression` includes `regex` and `regexp`
Example 1:
//Regular expression find word in string
In first example, we are using Regular expression to fing word in string using `\b`.
`\b` allows you to find a perticular word only using a regular expression in the form of \bword\b.
<?php
$name = 'I have 6 years of exprience';
preg_match( '/\byears\b/', $name, $match );
echo $match[0];
?>
Output: years
Example 2:
//Regular expression find numbers in string
In this example, we are using Regular expression to fing numbers in string using `/[0-9]/`.
`/[0-9]/` allows you to find a perticular numbers only using a regular expression in the form of `/[0-9]/`.
<?php
$name = 'I have 6 years of exprience';
preg_match( '/[0-9]/', $name, $match );
echo $match[0];
?>
Output: 6
Example 3:
//Regular expression for finding numbers/string in Bracket
In this example, we are using Regular expression to fing numbers/string in Bracket using `/\[(.*?)\]/`.
`/\[(.*?)\]/` allows you to find a numbers/string in Bracket only using a regular expression in the form of `/\[(.*?)\]/`.
<?php
$name = 'I have [6 years] of exprience';
preg_match( '/\[(.*?)\]/', $name, $match );//regular expression for finding numbers/string in Bracket ([6years])
echo $match[1];
?>
Output: 6 years
Example 4:
//Regular expression for finding numbers with String (Experience) (6 years)
<?php
$name = 'I have 6 years of exprience';
preg_match( '/[0-9] years/', $name, $match );//regular expression for finding numbers with String (Experience) (6 years)
var_dump($match);
echo $match[0];
?>
Output: 6 years
In this section,I will explain how to find words,string,numbers in string using Regular expression.
It is very easy to match strings or patterns of characters in php using regular expresions.
A regular expression provides a matching strings of word,particular characters, numbers,or patterns of characters.Abbreviations for `regular expression` includes `regex` and `regexp`
Example 1:
//Regular expression find word in string
In first example, we are using Regular expression to fing word in string using `\b`.
`\b` allows you to find a perticular word only using a regular expression in the form of \bword\b.
<?php
$name = 'I have 6 years of exprience';
preg_match( '/\byears\b/', $name, $match );
echo $match[0];
?>
Output: years
Example 2:
//Regular expression find numbers in string
In this example, we are using Regular expression to fing numbers in string using `/[0-9]/`.
`/[0-9]/` allows you to find a perticular numbers only using a regular expression in the form of `/[0-9]/`.
<?php
$name = 'I have 6 years of exprience';
preg_match( '/[0-9]/', $name, $match );
echo $match[0];
?>
Output: 6
Example 3:
//Regular expression for finding numbers/string in Bracket
In this example, we are using Regular expression to fing numbers/string in Bracket using `/\[(.*?)\]/`.
`/\[(.*?)\]/` allows you to find a numbers/string in Bracket only using a regular expression in the form of `/\[(.*?)\]/`.
<?php
$name = 'I have [6 years] of exprience';
preg_match( '/\[(.*?)\]/', $name, $match );//regular expression for finding numbers/string in Bracket ([6years])
echo $match[1];
?>
Output: 6 years
Example 4:
//Regular expression for finding numbers with String (Experience) (6 years)
<?php
$name = 'I have 6 years of exprience';
preg_match( '/[0-9] years/', $name, $match );//regular expression for finding numbers with String (Experience) (6 years)
var_dump($match);
echo $match[0];
?>
Output: 6 years
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
find a numbers,
find a string,
find a text,
find and replace word,
regex find word,
regular expression find replace,
regular expression syntax,
Regular Expressions,
search and replace
.You can leave a response, or trackback from your own site.