What is regular expression in php?
In this tutorial you will learn how regular expressions work, as well as how to use them to perform pattern matching in an efficient way in PHP. Show
What is Regular ExpressionRegular Expressions, commonly known as "regex" or "RegExp", are a specially formatted text strings used to find patterns in text. Regular expressions are one of the most powerful tools available today for effective and efficient text processing and manipulations. For example, it can be used to verify whether the format of data i.e. name, email, phone number, etc. entered by the user was correct or not, find or replace matching string within text content, and so on. PHP (version 5.3 and above) supports Perl style regular expressions via its Let's begin with a brief overview of the commonly used PHP's built-in pattern-matching functions before delving deep into the world of regular expressions.
Note: The PHP Regular Expression SyntaxRegular expression syntax includes the use of special characters (do not confuse with the
HTML special characters). The characters that are given special meaning within a regular expression, are: The following sections describe the various options available for formulating patterns: Character ClassesSquare brackets surrounding a pattern of characters are called a character class e.g. Negated character classes can also be defined that match
any character except those contained within the brackets. A negated character class is defined by placing a caret ( You can also define a range of characters by using the hyphen (
The following example will show you how to find whether a pattern exists in a string or not using the regular expression and PHP
Similarly, you can use the
Tip: Regular expressions aren't exclusive to PHP. Languages such as Java, Perl, Python, etc. use the same notation for finding patterns in text. Predefined Character ClassesSome character classes such as digits, letters, and whitespaces are used so frequently that there are shortcut names for them. The following table lists those predefined character classes:
The following example will show you how to find and replace space with a hyphen character in a string using regular expression and PHP
Repetition QuantifiersIn the previous section we've learnt how to match a single character in a variety of fashions. But what if you want to match on more than one character? For example, let's say you want to find out words containing one or more instances of the letter p, or words containing at least two p's, and so on. This is where quantifiers come into play. With quantifiers you can specify how many times a character in a regular expression should match. The following table lists the various ways to quantify a particular pattern:
The regular expression in the following example will splits the string at comma, sequence of commas, whitespace, or combination thereof using the PHP
Position AnchorsThere are certain situations where you want to match at the beginning or end of a line, word, or string. To do this you can use anchors. Two common anchors are caret (
The regular expression in the following example will display only those names from the names array which start with the letter "J" using the PHP
Pattern ModifiersA pattern modifier allows you to control the way a pattern match is handled. Pattern modifiers are placed directly after the regular expression, for example, if you want to search for a pattern in a case-insensitive manner, you can use the
The following example will show you how to perform a global case-insensitive search using the
Similarly, the following example shows how to match at the beginning of every line in a multi-line string using
Word BoundariesA word boundary character ( Similarly, the regexp The following example will highlight the words beginning with car in bold:
We hope you have understood the basics of regular expression. To learn how to validate form data using regular expression, please check out the tutorial on PHP Form Validation. What are types of regular expression in PHP?PHP offers two sets of regular expression functions: POSIX Regular Expression. PERL Style Regular Expression.
What is regular expression with example?Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*
What is the regular expression?A Regular Expression (or Regex) is a pattern (or filter) that describes a set of strings that matches the pattern. In other words, a regex accepts a certain set of strings and rejects the rest.
What is regular expression types?There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression. A few utilities like awk and egrep use the extended expression. Most use the "basic" regular expression.
|