我想要一个正则表达式,它将单引号包围的文本提取到数组中。例如,此正则表达式将提取括号之间的文本:
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-z
或A-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!'"]
答案 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)
我建议你这个正则表达式
'[^']+'