2

While going through the ruby-doc for regular expressions, I came across this example for implementing the && operator:

/[a-w&&[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))
# This is equivalent to:
/[abh-w]/

I understand that

/[a-w&&[^c-g]]/ 

would equate to

/[abh-w]/

because the "^" denotes symbols that should be excluded from the regular expression.

However, I am wondering about why "z" is not also included? Why was the equivalent regular expression NOT:

/[abh-wz]/

I am very new to regular expressions, much less any specifics for regular expressions within Ruby, so any help is greatly appreciated!

4

1 回答 1

0

该页面明确表示:

/[a-w&&[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))
# This is equivalent to:
/[abh-w]/

“z”不包含在左侧“AND”项中,因此无法匹配。

请参阅:“所有既是苹果,又是苹果或梨的事物”不包括梨。只有苹果既是苹果又是(苹果或梨)。同样,ais 在a-w[^c-g]z中,所以它匹配;z不在左边,所以不满足“AND”,因此整个表达式失败。

于 2013-08-06T22:27:44.397 回答