use strict;
my $str = '# This is test program 1. This program does nothing useful.
add $s0, $0, $zero
addi $t0, $t0, 100#test comment
addi $a0, $a0,100
test: add $s0, $s0, $a0 # this is a comment
addi $a0, $a0, -1
bne $a0, $0, test # this is another comment
addi $a0, $a0, 100
test1:
#test comment
lw $a0, 8($a1)
sw $a0,4($a1)
j test1
jr $ra
jal test1
slt$t0,$a0,$a1
beq$t0,$t1,test
sub $t3, $t1, $t1
sll $a0, $a1, 2
';
my $regex = qr/\$[A-Za-z]{0,4}[0-9]{0,1}/p;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html