Java but also iPHP offers a number of functions that use regular expressions to work with text. In the article, we will show their basic use for checking the format of a text string.
First of all, let’s recall that PHP offers functions working with two types of regular expressions: POSIX-extended expressions and Perl-compatible expressions . POSIX features were deprecated in PHP 5.3.0 and developers are advised to use only Perl features. However, we will still focus on the POSIX standard, because it is a bit simpler and therefore more suitable for interpretation. The transition to Perl functions is then not at all difficult and we will deal with it in the next article.
A regular expression is a formally written pattern against which a test string is compared. The tested string may or may not match the pattern, and multiple strings may match the same pattern. A simple example of use would be user input testing. Let’s say that we are processing an HTML form in which the user was supposed to enter a date and possibly a time. With a properly written regular expression, we can easily test whether the string filled by the user is in the required format. So let’s see what such a regular expression looks like.
Regular expression syntax
A regular expression is a string with a specific syntax. We understand most characters (e.g. letters and numbers) as ordinary characters . However, some characters have a special meaning – the so-called metacharacters . We will discuss all metacharacters in turn in the following paragraphs. If we need to write a metacharacter so that it is taken as ordinary, we add a backslash before it. For example, the dot character ‘.’represents any symbol in a regular expression. If we wanted to use a dot as an ordinary character, we must write it as ‘\.’.
Here it is also worth recalling the rules for writing strings in PHP. Let’s not forget that when writing a string in quotes, some russia phone number data sequences are replaced. E.g. “\n”is replaced for line breaks. It is therefore better to write regular expressions in apostrophes, where we only have to pay attention to the apostrophe and backslash character.
The simplest regular expression consists
of only ordinary characters. All strings that have the given expression as a substring match such an expression. Take for example the
Metacharacters ^and $represent the beginning or end of chain. By using them, we specify that the given expression should be found combining both strategies is ideal only at the beginning, at the end, or correspond to the entire string.
Reg. expression Matching strings
‘^Ahoj’ strings that start with Hello
‘Nashledanou$’ strings that end in Goodbye
‘^hroch$’ just a string containing just the word hippo with no other characters
Joker period
However, in some cases we don’t want to specify the given substring exactly. We want to leave some characters arbitrary, or limit them cg leads to characters from a certain set. We use two tools for this. The first of these is the placeholder .he dot serves as a joker that matches any one character.n databases.