Construct regular expressions in Perl for the following Any

Construct regular expressions in Perl for the following:

• Any string that contains an ‘a’ or ‘b’ followed by at least one digit.

• Any 5 digit integer.

• Any string whose length is a multiple of 5.

• A sentence (begins with a capital letter and ends with either a . , ? , or ! ).

• A Perl scalar variable name (including the ‘$’). Perl variable names can contain any alphanumeric character and the ‘_’ character.

Solution

Any string that contains an ‘a’ or ‘b’ followed by at least one digit.

Regular expression:     .*[a-b][0-9].*

Explanation:     .* in initial shows anything can come then followed by either a or b then any digit which is represented by [0-9] and after that any string can occur

Uses:

use strict;

my $str = \'\';

my $regex = qr/.*[a-b][0-9].*/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

               

               

Any 5 digit integer.

Regular expression:     [0-9]{5}

Explanation:     The expression [0-9] shows any digit between 0 to 9 and then {5} will count occurrence 5 times.

Uses:

use strict;

my $str = \'\';

my $regex = qr/[0-9]{5}/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

               

               

Any string whose length is a multiple of 5.

Regular expression:     ^(?:.{5})*$

Explanation:     (?:.{5})* repeats the inner part of the group 0 or more times ==> this will also match on the empty string! If you don\'t want this and start only from a string length of at least 5 use the + quantifier means 1 or more: ^(?:.{5})+$

Uses:

use strict;

my $str = \'\';

my $regex = qr/^(?:.{5})*$/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

               

               

A sentence (begins with a capital letter and ends with either a . , ? , or ! ).

Regular expression:     ^[A-Z].*(\\.|\\?|!)$

Explanation:     Here A-Z shows the capital letter regex matches with ASCII value so any thing comes in mid would be matched and then operator | represent or symbol between all 3.

Uses:

use strict;

my $str = \'\';

my $regex = qr/^[A-Z].*(\\.|\\?|!)$/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

               

               

A Perl scalar variable name (including the ‘$’). Perl variable names can contain any alphanumeric character and the ‘_’ character.

Regular expression:     ^\\$([a-z]|[A-Z]|[0-9]|\\_)+$

Explanation:     It starts with $ it represent by ^$ and then any digit, letter or underscore to validate the variable name

Uses:

use strict;

my $str = \'\';

my $regex = qr/^\\$([a-z]|[A-Z]|[0-9]|\\_)+$/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

               

               

use strict;

my $str = \'\';

my $regex = qr/.*[a-b][0-9].*/p;

if ( $str =~ /$regex/g ) {

print \"Matched\";

}

Construct regular expressions in Perl for the following: • Any string that contains an ‘a’ or ‘b’ followed by at least one digit. • Any 5 digit integer. • Any s
Construct regular expressions in Perl for the following: • Any string that contains an ‘a’ or ‘b’ followed by at least one digit. • Any 5 digit integer. • Any s
Construct regular expressions in Perl for the following: • Any string that contains an ‘a’ or ‘b’ followed by at least one digit. • Any 5 digit integer. • Any s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site