PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

Rule 1

The match that begins earliest wins.
 
$text = "The dragging belly indicates your cat is to fat";

$text =~ s/(fat|cat|belly|your)/X/;

print $text . "\n"; 

    #Outputs: The dragging X indicates your cat is to fat
    
Rule 2    (2/3)

Rule 2

The standard quantifiers (?, *, +, and {min,max}) are greedy. They always attempt to match as much as possible. They settle for less only when matching too much ends up causing the regex to fail.

Example

The \w+ alone is happy to match the entire word. But, if it does, it leaves nothing for "s" to match.
 
$text = "regexes";

$text =~ m/(\w+)s/;

print $1 . "\n"; #Outputs: regex
    
Too Much    (3/3)

Too much

Regex ^.*([0-9][0-9]) finds the last two digits on a line, and saves them to $1. First, .* matches the entire line. Next the folowing ([0-9][0-9]) is required. It fails to match at the end of the line, and yells to .* - "Hey, you took to much!" Give me back something so that I can have a change to match.
 
$text = "about 24 characters long";

$text =~ m/^.*([0-9][0-9])/; 

    # .* is forced to give up 'g', then 'n', ..., until 4

print $1; #Outputs: 24
    

Questions    
Match in PHP

        A B C D E F
🔔
1/2