Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能有一个完整的或Ruby中的正则表达式?_Ruby_Regex_String - Fatal编程技术网

我怎样才能有一个完整的或Ruby中的正则表达式?

我怎样才能有一个完整的或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

我想检查一个非字母/数字或\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 part on it's own.

谢谢

是的。使用正则表达式中的
|

/^\W|\W$/