Chris
Chris

Reputation: 12181

RegExp error in Perl

Binding a regexp to a parameter in perl, I'm getting the following error: syntax error at ./ctime.pl line 23, near "/^\((\d{2}):(\d{2})\)/)" Execution of ./ctime.pl aborted due to compilation errors.

Here is the line where I try to bind it:

$_[0] =~ /^\((\d{2}):(\d{2})\)/)/;

I'm trying to match an hours/minutes combination in parens, such as (99:99 or (01:24). There will always be four digits.

Upvotes: 1

Views: 134

Answers (2)

Joni
Joni

Reputation: 111219

You have an extra /) just before the end. The offending line should be:

$_[0] =~ /^\((\d{2}):(\d{2})\)/;

Upvotes: 2

ikegami
ikegami

Reputation: 385565

$_[0] =~ /^\((\d{2}):(\d{2})\)/)/;

should be

$_[0] =~ /^\((\d{2}):(\d{2})\)/;

You accidentally duplicated two characters near the end of the line.


PS — As a note of caution, you'll have problems if you try to pass $1 or the like as an argument to function that contains this code because you use $_[0] directly.

sub f {
   print("$_[0]\n");              # a
   print $_[0] =~ /a/ ?1:0,"\n";  # 1
   print("$_[0]\n");              # [empty string]
   print $_[0] =~ /a/ ?1:0,"\n";  # 0
}
'a'=~/(.)/;
f($1);

Upvotes: 9

Related Questions