Regular Expressions

PHP supports 2 types of regular expressions: POSIX and those used in Perl. Regular expressions are strings that can be adapted to various other strings. This can be compared to the so-called. wildcardów used for file operations, where the asterisk (* ‚) substitute for any number of any characters or expression *. *’ will match files with any name and any extension.

In regular expressions work as it were, back ‚- describes any number of repetitions (zero or more) of the preceding character, or phrase, and *’ will match, aaa ‚,’ aaaaa ‚but, b’, because in this expression there is no limit it can not be anything but, and ‚. Such a restriction can be achieved by signs, ^ ‚and’ $ ‚. Sign, ^ ‚it is understood as the beginning of the line, and, $’ as the end. So if we want to find a string that will be empty or will be in the same letter, and ‚it should look like this: ^ a * $’. However, if, a ‚must occur at least once, instead of character *’ must use the sign, + ‚- it means one or more of the last character. You can use the character? ‚, Which means no or one occurrence of the preceding character atom, character or range. You can also specify a specific number of occurrences of the character, an atom or interval by using them immediately after braces. If you will have one number, it will be determined the exact given number of repetitions. If you will be in their number, followed by a comma, the expression must be found at least as many as the specified number. You may be given in brackets two numbers separated by a comma, which means repeating the expression minimally as many times as the first number, and up to as much as the other (inclusive).
Instead of a single character, you can specify the range of words, or the entire expression – the so-called. atom. The range of characters is given in square brackets. In these brackets, you can specify individual characters one after the other (eg., [EGT] ‚), the range of letters (eg. All lowercase letters: [az]’ or a combination of the two types (eg., [A-zA-Z] ‚specifies all the letters). the square brackets can also be administered symbols. If the interval is required hyphen that normally is used for the determination of ranges of characters, you have to put it on the first or last position in brackets. The square brackets mark, ^ ‚has a different meaning. It means denial, that is, if you put them in front of a sign or a range, this character or range will not be able to appear in this place.
If the character can be any, instead of a particular letter, sign or symbol interval, you can use a period (.. ‚). For example, the expression ^. * $ ‚Describes any string consisting of any number of arbitrary characters.
Atom – expression contained in the parentheses can contain (almost) any number of characters, ranges and other atoms. This allows the matching of repeated fragments of strings. A good example is to check whether the string is a path in Unix system. An example of this string, / usr / local / bin / php ‚. The regular expression should look like this: (/ ([A-Z]) +) * (/ ([a-Z \.])) ‚. The first atom specifies any number of folders and then the file name – the atom there is no sign specifying the number of occurrences, and so it must occur exactly once.
Between atoms, marks or ranges you can put a sign | ‚, which means a logical operation, OR’, which means that there can be one or the other atom (the front of the sign, | ‚and after this).
If the regular expression you must enter a character that would be interpreted otherwise than going to (eg. If you need to find a string that starts with an asterisk after which it is any number of letters A, you can not normally ‚specify an asterisk, because it would be interpreted as any number the last character), you must replace the mark on so-called. , Escape sequence ‚- to be in front of this sign put a backslash (\’). Such characters are (,,,) ‚, [,,,]’, {,,,} ‚,’ \ ‚,’ * ‚,’ | ‚,’ ^ ‚,’ $ ‚,’? ‚. If the expression you need to use just a slash, then enter the two slashes, one after the other.

Close Menu