As the title says, does the regex pattern (x|y)*
match the same string as [xy]*
?
The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”. The main advantage is that the user can easily write in shorter form and can easily read it.
Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user. Syntax: s/text/pattern.
=~ is the Perl binding operator. It's generally used to apply a regular expression to a string; for instance, to test if a string matches a pattern: if ($string =~ m/pattern/) {
*. * , returns strings beginning with any combination and any amount of characters (the first asterisk), and can end with any combination and any amount of characters (the last asterisk). This selects every single string available.
Yes, they match the exact same set of strings.
They are not equivalent. (x|y)*
sets a backreference, [xy]*
doesn't.
Thus (?:x|y)*
and [xy]*
are equivalent in behavior, as neither sets a backreference.
It's close to equivalent, but the first form makes a capture from the group delimited by ( )
that can be retrieved with $1
(for the first one) when the regex
match.
If you want to avoid capturing, use
(?:re)
Where re
is the regex.
this only works if x
and y
are exactly x
and y
, not if they are general regexes
See Backtracking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With