from the /g at the end i agree it looks like it could be a malformed attempt at an awk/perl/etc substitution operation, and your rewrite of it as an s/// does work, but the parts between the ^ and $ would also be a valid regexp in Perl-compatible regexp and some other dialects if not for the spaces at the start and end. And, the /g is also a flag (“Match globally, i.e., find all occurrences.”) for the m/// matching operator in Perl.
The \1 and \2 are backreferences to the capture groups, which can be used not only in the replacement part but also in the pattern itself.
…which will echo the string because it matches the pattern. (if you edit the input string to change, for instance, the last digit, it will no longer match and will output nothing.)
There is no input that can match the pattern as it is in the comic with the space before the ^ and after the $ however.
Interestingly backreferences are also supported by POSIX Basic Regular Expressions (BRE), but are not supported by POSIX Extended Regular Expressions (ERE). (Also the former requires you to escape parenthesis and curly braces for them to become meta characters, while the latter requires you to escape them if they’re literals as Perl etc do. And neither of the POSIX flavors supports \d as a shortcut for [0-9].)
from the /g at the end (and the spaces on the edges) i agree it looks like a malformed attempt at an awk/perl/etc substitution
The /g at the end is the global operater. It means, roughly, match across the entire input string.
This is completely valid regex, not a malformed attempt at anything. It’s just that the delimiters and operators are often omitted from regex in practical use so you may not be used to seeing them.
yeah, i edited my comment while you were replying to note that /g is a valid flag for m/// as well. it is a valid perl matching operation precisely as-is but it can’t match anything due to the spaces it has before the ^ and after the $.
from the
/gat the end i agree it looks like it could be a malformed attempt at an awk/perl/etc substitution operation, and your rewrite of it as ans///does work, but the parts between the^and$would also be a valid regexp in Perl-compatible regexp and some other dialects if not for the spaces at the start and end. And, the/gis also a flag (“Match globally, i.e., find all occurrences.”) for them///matching operator in Perl.The
\1and\2are backreferences to the capture groups, which can be used not only in the replacement part but also in the pattern itself.You can see this working by running this command:
echo '123 - 45 - 67890 45 123'|perl -ne 'print if m/^(\d{3}) - (\d{2}) - (\d{5}) \2 \1$/g'…which will echo the string because it matches the pattern. (if you edit the input string to change, for instance, the last digit, it will no longer match and will output nothing.)
There is no input that can match the pattern as it is in the comic with the space before the
^and after the$however.Interestingly backreferences are also supported by POSIX Basic Regular Expressions (BRE), but are not supported by POSIX Extended Regular Expressions (ERE). (Also the former requires you to escape parenthesis and curly braces for them to become meta characters, while the latter requires you to escape them if they’re literals as Perl etc do. And neither of the POSIX flavors supports
\das a shortcut for[0-9].)The /g at the end is the global operater. It means, roughly, match across the entire input string.
This is completely valid regex, not a malformed attempt at anything. It’s just that the delimiters and operators are often omitted from regex in practical use so you may not be used to seeing them.
yeah, i edited my comment while you were replying to note that
/gis a valid flag form///as well. it is a valid perl matching operation precisely as-is but it can’t match anything due to the spaces it has before the^and after the$.