简体   繁体   English

使用正则表达式将字符串拆分为单词

[英]Splitting string into words using regular expression

I need some help making sense of the following Ruby code for splitting a sentence into words. 我需要一些帮助来理解以下Ruby代码,以将句子拆分为单词。

class String
  def words
    scan(/\w[\w\'\-]*/)
  end
end

Here is the method in action: 这是起作用的方法:

"This is a test of words' capabilities".words

returns 退货

["This", "is", "a", "test", "of", "words'", "capabilities"]

What does the regular expression (/\\w[\\w\\'-]*/) mean exactly? 正则表达式(/ \\ w [\\ w \\'-] * /)的确切含义是什么?

Basically that translates to: 基本上可以转化为:

/ : indicates start of regular expression /:表示正则表达式的开始
\\w : Find me a word character (typically [A-Za-z0-9_]) \\ w:找到一个文字字符(通常为[A-Za-z0-9_])
[\\w\\'-]* : then find me zero or more word characters, apostrophes, or dashes [\\ w \\'-] *:然后找到零个或多个单词字符,单引号或破折号
/ : indicates end of regular expression /:表示正则表达式的结尾

The Ruby code is finding every match for that expression (any unbroken series of word characters that can contain apostrophes and dashes but can't start with them), and storing that into an array as it finds it. Ruby代码正在查找该表达式的所有匹配项(可以包含撇号和破折号但不能以它们开头的任何连续的单词字符序列),并将其存储到数组中。

I'm sure you can find the Regular expressions document. 我确定您可以找到正则表达式文档。 A useful resource to answer your question and learn more about it in general! 一个有用的资源,可以回答您的问题并总体上了解更多信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM