If you are developing in Perl, PHP, Python, Ruby, JavaScript (or any other language with regular expression support) and do not know regular expressions, you miss a lot. And if you deliberately avoid regular expressions, these are like torn pages from your manual, although the rest of it is in place.
Regular expressions are powerful tools.
I am sure you have heard this statement before. But you also heard a lot of negativity on stackoverflow or somewhere else. There are two main reasons for this:
- Inappropriate use of regular expressions
- Misunderstanding and fear of regular expressions

Perhaps the most common example of inappropriate use of a regular expression is checking email addresses. For example, let's say you want to verify an email address by simply checking the @ symbol. The use of preg_match regular expressions ('/ @ /', $ E-mail) is redundant. There are no regular expressions. We were just looking for a symbol. In this case, it would be more appropriate to use strpos ($ email, '@') I say “more appropriate” and not “better performance”, because in this example, where we work with a tiny email string. Losses are insignificant. However, if we search for a very large string instead of an e-mail address and do it several times during a cycle, this will lead to a significant performance improvement. For example, you need to find a few lines in the html source code of the page.
If you are searching for some constant string - do not use regular expressions! Unfortunately, some developers did not understand what this means, but simply decided that regular expressions are not terrible productive and that they should avoid regular expressions at all costs. Some of them even use it in the argument not to study regular expressions. This is absolutely wrong! Let's add some small requirements to our example. Let's say we want to make sure that the @ character is the first character in the string, and if so, replace it with the # character. Maybe we are converting Tweet responses to hash tags for some reason. Using string functions, it might look something like this:
if ( 0 === strpos( $string, '@' ) ) { $string = '#' . substr( $string, 1 ); }
But with the help of regular expressions, the same can be achieved more concisely:
$string = preg_replace( '/^@/', '#', $string );
If you don’t know regular expressions yet, this may seem less readable to you, but you should understand that it is more convenient for those who have the power to use regular expressions to read. If we add another requirement to our example: the last character should also be @, and only then we will replace both @ characters with #. Then string functions further complicate our code, and make it less readable:
$length = strlen( $string ); if ( 0 === strpos( $string, '@' ) && $length - 1 === strrpos( $string, '@' ) ) { $string = '#' . substr( $string, 1, $length - 1 ) . '#'; }
Our regular expression changes slightly:
$string = preg_replace( '/^@(.*)@$/', '#$1#', $string );
These are trivial examples, but hopefully they show how the code becomes more complex if you insist on using string functions instead of answering the call of writing regular expressions. So the next time you find yourself choosing a way to avoid regular expressions, just look for solutions, find out how to do this with regular expressions, tune it, play with it, fight it, and repeat. This is the best way to learn!
Transfer