Perl has regular expressions built into the language

Perl has regular expressions built into the programming language. Therefore, it is possible to write the character string processing that appears frequently in a short and high speed.

Regular expression syntax in pattern matching

In string processing, you often want to see if the characters in this pattern are included in the string. With Perl, regular expressions are built into the language, so you can write regular expressions using pattern matching syntax.

#Regular expression pattern matching syntax
if ($str = ~ / regular expression /) {

}

Repeated pattern matching

Since regular expressions are built into the language, parsing also parses the regular expression syntax. There is a syntax that makes it easy to write pattern matching iterations.

while ($str = ~ / (regular expression) / g) {
  my $match = $1;
}

You can easily write repetitive pattern matching by combining the while statement with the g option of the regular expression. As an internal implementation, there is a variable that saves the start position of the character string at the timing of repetition.

Regular expression syntax for permutations

Perl has a regular expression syntax for substitutions. Perl has adopted the syntax sed. You can replace strings with a short syntax, just like using the Linux command sed. increase.

#Regular expression replacement syntax
$str = ~ s / regular expression / replaced string /;

Can you handle Japanese correctly with regular expressions?

Yes, you can handle Japanese correctly with regular expressions. To handle Japanese correctly, please refer to the following articles.

Encode module-Properly handle multibyte strings such as Japanese

By converting to a decoded character string, Japanese can be handled correctly with regular expressions.

Are there any other programming languages ​​that have regular expressions built in?

In addition to Perl, Ruby is another programming language that incorporates regular expressions. Ruby uses Perl's regular expression grammar.

# Ruby pattern matching syntax
String = ~ / regular expression /

Many languages ​​provide regular expressions as libraries.

Associated Information