简体   繁体   English

尝试使用匹配器/ \\ w /和/ \\ W /将句子拆分为单词和定界符

[英]Trying to split a sentence into words and delimiters using matchers /\w/ and /\W/

I'd like to split a sentence into words and the parts between the words (I call them delimiters). 我想将一个句子分为单词和单词之间的部分(我称它们为定界符)。

sentence = "First-tea,-then-coffee!"
=> "First-tea,-then-coffee!"
words = sentence.split(/\W+/) # Splits by non-word characters
=> ["First", "tea", "then", "coffee"]
delimiters = sentence.split(/\w+/) # Splits by word characters
=> ["", "-", ",-", "-", "!"]

Splitting into words works fine, but I'm having a question about the delimiters. 分割成单词效果很好,但是我对分隔符有疑问。

Where does this first empty string come from in the delimiter array? 第一个空字符串从定界符数组何处来?

Thanks for explanation. 感谢您的解释。

Between start of line /^/ and first occurrance of - there is "First" . 在行/^/和of的第一次出现之间-存在"First"

So it splits on "First" obtaining an empty string "" and - . 因此,它拆分为"First"获得一个空字符串""-

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

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