Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex - Is (x|y)* equivalent to [xy]*?

Tags:

As the title says, does the regex pattern (x|y)* match the same string as [xy]*?

like image 438
user984152 Avatar asked Feb 25 '13 03:02

user984152


People also ask

What does \d mean in Perl?

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.

What does S * mean in Perl?

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.

What does =~ mean in Perl?

=~ 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/) {

What is the use of * in regular expression?

*. * , 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.


2 Answers

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.

like image 188
OmnipotentEntity Avatar answered Oct 16 '22 08:10

OmnipotentEntity


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.

Note

this only works if x and y are exactly x and y, not if they are general regexes

See Backtracking

like image 40
Gilles Quenot Avatar answered Oct 16 '22 10:10

Gilles Quenot