将单词引号的文本拆分为不是单词的一部分

时间:2014-03-08 07:20:32

标签: ruby regex

我想要一个正则表达式,它将单引号包围的文本提取到数组中。例如,此正则表达式将提取括号之间的文本:

string = "(Well!) thought Alice to herself, (after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!)"
string.scan(/\((?>[^\(\)\\]+|\\{2}|\\.)*\)/)
# => ["(Well!)", "(after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!)"] 

我想用单引号做同样的事情。我需要忽略a-zA-Z范围内的字符前面和后面的单引号(如同它是收缩的一部分而不用作引号)。

string = "'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!'"
# => ["'Well!'", "'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!'"] 

我试过了:

string.scan(/'(?>[^'\\]+|\\{2}|\\.)*'/)
# => ["'Well!'", "'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'", "'t say anything about it, even if I fell off the top of the house!'"] 

3 个答案:

答案 0 :(得分:2)

string.scan(/(?<![a-zA-Z])'(?:[^']|'[a-zA-Z])*'(?![a-zA-Z])/)
# =>
# [
#    "'Well!'",
#    "'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!'"
# ]

答案 1 :(得分:1)

string = "'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!'"

p string.scan(/\B'.*?'\B/) #=> ["'Well!'", "'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!'"]

答案 2 :(得分:-3)

我建议你这个正则表达式

'[^']+'