More Examples of Perl Regular Expressions

Some Example of Basic Perl Regular Expressions

We continue of examples of Perl regular expressions. On this page we show, among other things, the effect of qualification {n,m} and grouping. Note how this works in a greedy fashioned. This is to say, it matches the largest number of groups or characters it can, consistent with the {n,m} requirement. However in the more complex case, where a shorted match occurs followed by some other sub-string then a longer possible match. The first found, shorter choice will be the returned match. So that matching stops when largest possible first match is found.

Example regular expression that matches four digits

This example shows a Perl regular expression that matches four digits. Shown here and similarly in the examples below are, the results of a match. We see, the part of the input before the match,the characters actually matched, and the characters after the match. In some examples all of these are empty indicating there was no match found.We start with a very simple method to match a fixed number of digits. The simplest way to match any four digits in a row.

Example 1: matches four digits \d\d\d\d
regexp input pre match $` match $& post match $'
\d\d\d\d lower case (alpha str)
\d\d\d\d UPPER CASE (ALHPA CHAR)
\d\d\d\d 12887887887887887887887_8 1288 7887887887887887887_8
\d\d\d\d (_1(It#:,,: UPaIT))51
\d\d\d\d (887)(887)(887)(0478)2 (887)(887)(887)( 0478 )2
\d\d\d\d meta(888)[887]{887}##::,,/\(}-{)
\d\d\d\d (www.review-pc.com)
\d\d\d\d l8878887887887.26(in)a4_ l 8878 887887887.26(in)a4_

Example regular expression that matches four digits

This example shows a Perl regular expression that matches four digits. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. A second way to match any four digits in a row.

Example 2: matches four digits \d{4}
regexp input pre match $` match $& post match $'
\d{4} lower case (alpha str)
\d{4} UPPER CASE (ALHPA CHAR)
\d{4} 12887887887887887887887_8 1288 7887887887887887887_8
\d{4} (_1(It#:,,: UPaIT))51
\d{4} (887)(887)(887)(0478)2 (887)(887)(887)( 0478 )2
\d{4} meta(888)[887]{887}##::,,/\(}-{)
\d{4} (www.review-pc.com)
\d{4} l8878887887887.26(in)a4_ l 8878 887887887.26(in)a4_

Example regular expression that matches between two and four digits

This example shows a Perl regular expression that matches between two and four digits. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. We can match 2 3 or 4 digits in a row.

Example 3: matches between two and four digits \d{2,4}
regexp input pre match $` match $& post match $'
\d{2,4} lower case (alpha str)
\d{2,4} UPPER CASE (ALHPA CHAR)
\d{2,4} 12887887887887887887887_8 1288 7887887887887887887_8
\d{2,4} (_1(It#:,,: UPaIT))51 (_1(It#:,,: UPaIT)) 51
\d{2,4} (887)(887)(887)(0478)2 ( 887 )(887)(887)(0478)2
\d{2,4} meta(888)[887]{887}##::,,/\(}-{) meta( 888 )[887]{887}##::,,/\(}-{)
\d{2,4} (www.review-pc.com)
\d{2,4} l8878887887887.26(in)a4_ l 8878 887887887.26(in)a4_

Example regular expression that matches between 0 and four digits

This example shows a Perl regular expression that matches between 0 and four digits. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Matches 0 1 2 3 or four digits in a row. So this includes no digits at all. You can see this particularly with alpha only strings. In effect we match the empty character before the start of the string proper.

Example 4: matches between 0 and four digits \d{0,4}
regexp input pre match $` match $& post match $'
\d{0,4} lower case (alpha str) lower case (alpha str)
\d{0,4} UPPER CASE (ALHPA CHAR) UPPER CASE (ALHPA CHAR)
\d{0,4} 12887887887887887887887_8 1288 7887887887887887887_8
\d{0,4} (_1(It#:,,: UPaIT))51 (_1(It#:,,: UPaIT))51
\d{0,4} (887)(887)(887)(0478)2 (887)(887)(887)(0478)2
\d{0,4} meta(888)[887]{887}##::,,/\(}-{) meta(888)[887]{887}##::,,/\(}-{)
\d{0,4} (www.review-pc.com) (www.review-pc.com)
\d{0,4} l8878887887887.26(in)a4_ l8878887887887.26(in)a4_

Example regular expression that matches any word character at the end of a string

This example shows a Perl regular expression that matches any word character at the end of a string. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Remember \w can include \d and _.

Example 5: matches any word character at the end of a string \w{3}$
regexp input pre match $` match $& post match $'
\w{3}$ lower case (alpha str)
\w{3}$ UPPER CASE (ALHPA CHAR)
\w{3}$ 12887887887887887887887_8 1288788788788788788788 7_8
\w{3}$ (_1(It#:,,: UPaIT))51
\w{3}$ (887)(887)(887)(0478)2
\w{3}$ meta(888)[887]{887}##::,,/\(}-{)
\w{3}$ (www.review-pc.com)
\w{3}$ l8878887887887.26(in)a4_ l8878887887887.26(in) a4_

Example regular expression that matches any non-word character at the end of a string

This example shows a Perl regular expression that matches any non-word character at the end of a string. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Remember \W means not \w and is different from the literal W.

Example 6: matches any non-word character at the end of a string \W{3}$
regexp input pre match $` match $& post match $'
\W{3}$ lower case (alpha str)
\W{3}$ UPPER CASE (ALHPA CHAR)
\W{3}$ 12887887887887887887887_8
\W{3}$ (_1(It#:,,: UPaIT))51
\W{3}$ (887)(887)(887)(0478)2
\W{3}$ meta(888)[887]{887}##::,,/\(}-{) meta(888)[887]{887}##::,,/\(} -{)
\W{3}$ (www.review-pc.com)
\W{3}$ l8878887887887.26(in)a4_

Example regular expression that matches two to four non-word character at the end of a string

This example shows a Perl regular expression that matches two to four non-word character at the end of a string. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Here we see the effect of \W qualification and the $ anchor.

Example 7: matches two to four non-word character at the end of a string \W{2,4}$
regexp input pre match $` match $& post match $'
\W{2,4}$ lower case (alpha str)
\W{2,4}$ UPPER CASE (ALHPA CHAR)
\W{2,4}$ 12887887887887887887887_8
\W{2,4}$ (_1(It#:,,: UPaIT))51
\W{2,4}$ (887)(887)(887)(0478)2
\W{2,4}$ meta(888)[887]{887}##::,,/\(}-{) meta(888)[887]{887}##::,,/\( }-{)
\W{2,4}$ (www.review-pc.com)
\W{2,4}$ l8878887887887.26(in)a4_

Example regular expression that matches any the literal string www

This example shows a Perl regular expression that matches any the literal string www. As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Simply the three characters www anywhere in a string.

Example 8: matches any the literal string www w{3}
regexp input pre match $` match $& post match $'
w{3} lower case (alpha str)
w{3} UPPER CASE (ALHPA CHAR)
w{3} 12887887887887887887887_8
w{3} (_1(It#:,,: UPaIT))51
w{3} (887)(887)(887)(0478)2
w{3} meta(888)[887]{887}##::,,/\(}-{)
w{3} (www.review-pc.com) ( www .review-pc.com)
w{3} l8878887887887.26(in)a4_

Example regular expression that matches four groups of 887

This example shows a Perl regular expression that matches four groups of 887 . As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Here we are grouping with qualifiers {4} so we match four blocks.

Example 9: matches four groups of 887 (887){4}
regexp input pre match $` match $& post match $'
(887){4} lower case (alpha str)
(887){4} UPPER CASE (ALHPA CHAR)
(887){4} 12887887887887887887887_8 12 887887887887 887887887_8
(887){4} (_1(It#:,,: UPaIT))51
(887){4} (887)(887)(887)(0478)2
(887){4} meta(888)[887]{887}##::,,/\(}-{)
(887){4} (www.review-pc.com)
(887){4} l8878887887887.26(in)a4_

Example regular expression that matches one to four groups of 887

This example shows a Perl regular expression that matches one to four groups of 887 . As before we show, the part of the input before, the matched sub-string if any, and the post match sub-string.If all of these are empty then there was no match. Here we are grouping with qualifiers {1,4} so we match one to four blocks.

Example 10: matches one to four groups of 887 (887){1,4}
regexp input pre match $` match $& post match $'
(887){1,4} lower case (alpha str)
(887){1,4} UPPER CASE (ALHPA CHAR)
(887){1,4} 12887887887887887887887_8 12 887887887887 887887887_8
(887){1,4} (_1(It#:,,: UPaIT))51
(887){1,4} (887)(887)(887)(0478)2 ( 887 )(887)(887)(0478)2
(887){1,4} meta(888)[887]{887}##::,,/\(}-{) meta(888)[ 887 ]{887}##::,,/\(}-{)
(887){1,4} (www.review-pc.com)
(887){1,4} l8878887887887.26(in)a4_ l 887 8887887887.26(in)a4_

Example regular expression that matches one to three groups of any four digits

This example shows a Perl regular expression that matches one to three groups of any four digits. Lastly and as previous we see, the part of the input before, the matched sub-string if any, and the post match sub-string.Where these are all empty then there was no match found. Here we are grouping with qualifiers {1,3} so we match one to three blocks.

Example 11: matches one to three groups of any four digits (\d\d\d\d){1,3}
regexp input pre match $` match $& post match $'
(\d\d\d\d){1,3} lower case (alpha str)
(\d\d\d\d){1,3} UPPER CASE (ALHPA CHAR)
(\d\d\d\d){1,3} 12887887887887887887887_8 128878878878 87887887887_8
(\d\d\d\d){1,3} (_1(It#:,,: UPaIT))51
(\d\d\d\d){1,3} (887)(887)(887)(0478)2 (887)(887)(887)( 0478 )2
(\d\d\d\d){1,3} meta(888)[887]{887}##::,,/\(}-{)
(\d\d\d\d){1,3} (www.review-pc.com)
(\d\d\d\d){1,3} l8878887887887.26(in)a4_ l 887888788788 7.26(in)a4_

Summary

In the above examples we see some simple Perl like regular expressions for qualification and grouping. The greedy action of {n,m} qualification is also apparent in some of these examples. A good deal of what we have seen here is applicable to other languages. In particular, PHP, Ruby and Python. However, it needs to be stressed that languages such as AWK, grep and egrep, can behave very differently. These languages use a less flexible but significantly more efficient matching algorithms than does Perl.

Perl examples illustrated

  • Example 1.The regexp \d\d\d\d matches four digits.
  • Example 2.The regexp \d{4} matches four digits.
  • Example 3.The regexp \d{2,4} matches between two and four digits.
  • Example 4.The regexp \d{0,4} matches between 0 and four digits.
  • Example 5.The regexp \w{3}$ matches any word character at the end of a string.
  • Example 6.The regexp \W{3}$ matches any non-word character at the end of a string.
  • Example 7.The regexp \W{2,4}$ matches two to four non-word character at the end of a string.
  • Example 8.The regexp w{3} matches any the literal string www.
  • Example 9.The regexp (887){4} matches four groups of 887.
  • Example 10.The regexp (887){1,4} matches one to four groups of 887.
  • Example 11.The regexp (\d\d\d\d){1,3} matches one to three groups of any four digits.