mickeyj
mickeyj

Reputation: 101

Issue with matching

The following code below is trying to match a format like

[a=>]b[->c][d:e]

where a=>, ->c, d:e are optional.

($reg =~ /^
     (?:([\w\/]+)=>)?       # (optional)
     (\w+)                  # (required)
     (?:->(\w+))?           # (optional)
     (\[\d+\]|\[\d+:\d+\])? # (optional)
     .$/x)
        or croak ("-E Invalid register format );

When I give the input as sample=>STATUS as $reg value, the last S of STATUS is getting truncated. Why?

Upvotes: 1

Views: 59

Answers (2)

hoffmeister
hoffmeister

Reputation: 612

you need to add, and escape, the square brackets

my $regex=qr{^
 (?:(\[[\w\/]+)=>\])?       # (optional)
 (\w+)                  # (required)
 (?:\[->(\w+)\])?           # (optional)
 (?:\[\w+\]|\[\w+:\w+\])? # (optional)
    }x;

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70909

The regex symbol . just before your $ line-end symbol captures "one thing" which in your case, seems to be the last letter S

This means that your regex is almost right, but that "one thing" needed to be satisfied by the regex, so the regex matcher rewound the required (\w+) pattern by one character to give the . its demanded character.

Upvotes: 1

Related Questions