- Perl ›
- Regular expression ›
- here
Match the beginning and end of a string - ^and $
Use ^ and $ to represent the beginning and end of a string with a regular expression.
#^Represents the beginning. /^ab/ # $Represents the end / cd $/
^ Is also used as a character class negation symbol. If it is at the beginning of [], it represents the negation of the character class, and if it is at the beginning of //, it represents the beginning of the string.
Example
This is an example that matches the beginning and end of the string.
use strict;
use warnings;
# Represents the beginning and end of a string.^And $
my $word = "abcd";
print "1: Represent the beginning of the string.^\n";
if ($word =~ /^ab/) {
print "'$word' starts with ab.\n"
}
if ($word =~ /bc/) {
print "includes bc, but\n"
}
if ($word !~ /^Bc/) {
print "does not start with bc.\n\n";
}
print "2: Show the end of the string \.^\n";
if ($word =~ /cd$/) {
print "'$word' ends with cd.\n"
}
if ($word =~ /bc/) {
print "includes bc, but\n"
}
if ($word !~ /bc$/) {
print "does not end with bc.\n\n";
}
Perl ABC