Ruby正则表达式,用于提取不包含空格的字符串中的单词

时间:2016-09-27 02:16:02

标签: ruby regex

说我有字符串str = "ASimpleNoSpaceTitle"。我似乎无法理解如何使用正则表达式来分割和提取所有大写单词,以便得到["A", "Simple", "No", "Space", "Title"]

什么是正常表达才能完成这项工作?

更新:带有和没有空格/大写的单词串怎么样?与"ASimpleNoSpaceTitle and a subtitle"["A", "Simple", "No", "Space", "Title", "and", "a", "subtitle"]

一样

4 个答案:

答案 0 :(得分:6)

String#scancharacter class ranges一起使用,可以通过简单易懂的正则表达式获得您想要的内容:

str = "ASimpleNoSpaceTitle"
str.scan(/[A-Z][a-z]*/) # => ["A", "Simple", "No", "Space", "Title"]

您可以使用POSIX括号表达式[[:upper:]][[:lower:]],这样您的正则表达式也可以处理非ASCII字母,例如À或ç:

str = "ÀSimpleNoSpaçeTitle"
str.scan(/[A-Z][a-z]*/) # => ["Simple", "No", "Spa", "Title"]
str.scan(/[[:upper:]][[:lower:]]*/) # => ["À", "Simple", "No", "Spaçe", "Title"]

要允许单词以小写字母开头,而不是以其他字母开头,您可以使用此变体:

str = "ASimpleNoSpaceTitle and a subtitle"
str.scan(/[A-Za-z][a-z]*/) # => ["A", "Simple", "No", "Space", "Title", "and", "a", "subtitle"]
# OR
str.scan(/[[:alpha:]][[:lower:]]*/)

答案 1 :(得分:4)

"ABSimpleNoSpaceTitle".split(/(?=[[:upper:]])/)
  #=> ["A", "B", "Simple", "No", "Space", "Title"]

(?=[[:upper:]])积极向前看,要求匹配后跟大写字母。

答案 2 :(得分:2)

2016年这样做的正确方法是:

"ASimpleNoSpaceTitle and a subtitle".split(/(?=\p{Lu})|\s+/)
#⇒ ["A","Simple","No","Space","Title","and","a","subtitle"]

答案 3 :(得分:0)

这是一种方法。

在内置的scan()方法中传递此正则表达式。

regext 2048x1536

所有正则表达式都会找到一个大写字母/[[:upper:]](?:[[:lower:]]+)?/,后面跟一个小写字母[[:upper:]]

扫描将查找匹配字符串/ char..etc

的多个匹配项
(?:[[:lower:]]+)?