in reply to capturing optional text with a regex
G'day RonW,
Welcome to the monastery.
You really need to show your real test (and its results) or a short, self-contained example which reproduces what you're describing.
I'm wondering whether you're testing the truth of the match or the truth of the captures. Here's an example:
#!/usr/bin/env perl -l use strict; use warnings; while (<DATA>) { if (/\s([#]\S)(.*)/) { print 'MATCH: TRUE'; if ($1 && $2) { print '$1 && $2: TRUE'; } else { print '$1 && $2: FALSE'; } print "\$1='$1'"; print "\$2='$2'"; } else { print 'MATCH: FALSE'; } } __DATA__ 123/; #< 456/; #< a comment
Output:
MATCH: TRUE $1 && $2: FALSE $1='#<' $2='' MATCH: TRUE $1 && $2: TRUE $1='#<' $2=' a comment'
-- Ken
|
|---|