我怎样才能有一个完整的或Ruby中的正则表达式?
我想检查一个非字母/数字或\W是否在变量的开头或结尾,只是一个true或false。我通常使用的代码是:我怎样才能有一个完整的或Ruby中的正则表达式?,ruby,regex,string,Ruby,Regex,String,我想检查一个非字母/数字或\W是否在变量的开头或结尾,只是一个true或false。我通常使用的代码是: str.match( /^\W) || str.match( /\W$/ ) 当然,可以通过多种方式实现这一点,例如: [ /^\W/, /\W$/ ].index{ | regy | str.match( regy ) } 但是,我想知道在一个正则表达式中是否有实现这一点的方法。即 str.match( regy ) # the single regexp handles the or
str.match( /^\W) || str.match( /\W$/ )
当然,可以通过多种方式实现这一点,例如:
[ /^\W/, /\W$/ ].index{ | regy | str.match( regy ) }
但是,我想知道在一个正则表达式中是否有实现这一点的方法。即
str.match( regy ) # the single regexp handles the or part on it's own.
谢谢是的。使用正则表达式中的
|
:
/^\W|\W$/