in reply to Re: Capturing everything after an optional character in a regex?
in thread Capturing everything after an optional character in a regex?
And here's a little test -$string =~ m/(?:(?=.*?X)X|(?!.*?X))(\S+)/;
And the output is as expected, and both in $1 -$string1="abcX123"; $string2="abc123"; $string1 =~ m/(?:(?=.*?X)X|(?!.*?X))(\S+)/; print "$1\n"; $string2 =~ m/(?:(?=.*?X)X|(?!.*?X))(\S+)/; print "$1\n";
And the tricky bit in the above regex is the (?:(?=.*?X)X|(?!.*?X)) part, which defines an optional anchor point.123 abc123
|
|---|