Ruby 正则表达式提取单词
我目前正在努力想出一个正则表达式,它可以将字符串分割成单词,其中单词被定义为由空格包围或用双引号括起来的字符序列。我正在使用 String#scan
例如,字符串:
' hello "my name" is "Tom"'
应该匹配单词:
hello
my name
is
Tom
我设法使用以下方法匹配用双引号括起来的单词:
/"([^\"]*)"/
但我不知道如何合并包围的单词通过空格字符来获取“你好”、“是”和“汤姆”,同时不会搞砸“我的名字”。
任何对此的帮助将不胜感激!
I'm currently struggling to come up with a regex that can split up a string into words where words are defined as a sequence of characters surrounded by whitespace, or enclosed between double quotes. I'm using String#scan
For instance, the string:
' hello "my name" is "Tom"'
should match the words:
hello
my name
is
Tom
I managed to match the words enclosed in double quotes by using:
/"([^\"]*)"/
but I can't figure out how to incorporate the surrounded by whitespace characters to get 'hello', 'is', and 'Tom' while at the same time not screw up 'my name'.
Any help with this would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会为你工作。它将打印
Just 忽略空字符串。
解释
您可以像这样使用
reject
来避免打印空字符串
will work for you. It will print
Just ignore the empty strings.
Explanation
You can use
reject
like this to avoid empty stringsprints
产生:
解释:
0 个或多个空格后跟
单个
双引号内的一些单词或
单词
后跟 0 个或多个空格
Produces:
Explanation:
0 or more spaces followed by
either
some words within double-quotes OR
a single word
followed by 0 or more spaces
您可以尝试这个正则表达式:
它使用
\b
来查找单词边界。这个网站 http://rubular.com/ 很有帮助。You can try this regex:
which uses
\b
to find the word boundary. And this web site http://rubular.com/ is helpful.