Reputation: 3
print "Enter string:\n";
$sentence = <>;
print "Enter the regex to use for search:\n";
$regex = <>;
if ($sentence =~ $regex){
print "Matching\n";
}
else{
print "Not matching";
}
print "Match found: ", # This should show the text that is found by the regular expression that is typed by the user.
Hello, I am having trouble with my code. This is what I've been typing in the following fields. $sentence: The quick fox. $regex: q.*k. The $regex doesn't match with the $sentence. Do I have to convert the $regex? Also, how can I print the matching text in the end? Something like this.
Enter string:
The quick fox
Enter the regex to use for search:
q.*k
Matching
Match found: quick
Upvotes: 0
Views: 45
Reputation: 44283
You need to remove the trailing newline from the input regex:
print "Enter string:\n";
$sentence = <>;
chomp $sentence; # remove trailing newline (optional)
print "Enter the regex to use for search:\n";
$regex = <>;
chomp $regex; # remove trailing newline (not optional!!)
if ($sentence =~ $regex){
print "Matching: $&\n"; # Group 0
}
else {
print "Not matching\n";
}
Upvotes: 1