REGEX Ruby Rails
我有: 我试图生成一个数组,该数组包含四个元素:REGEX Ruby Rails,ruby,regex,Ruby,Regex,我有: 我试图生成一个数组,该数组包含四个元素: string = "4/28 - Declined 4/19 - Call with Bob, Joe and Steve 4/10 - Call scheduled for 10a on 4/18 4/9 - Wants to meet with Jay on 4/28 at 10:30a" 我遇到以下问题: 4/28 - Declined 4/19 - Call with Bob, Joe and Steve 4/10 - Call sch
string = "4/28 - Declined 4/19 - Call with Bob, Joe and Steve 4/10 - Call scheduled for 10a on 4/18 4/9 - Wants to meet with Jay on 4/28 at 10:30a"
我遇到以下问题:
4/28 - Declined
4/19 - Call with Bob, Joe and Steve
4/10 - Call scheduled for 10a on 4/18
4/9 - Wants to meet with Jay on 4/28 at 10:30a
我得到:
string.scan(/\d{1,2}+\/\d{1,2}[\s]?[-|:]+\s[A-Za-z\s\d]+ (?![\d{1,2}\/\d{1,2}]* -)/)
请尝试此regexp:
["4/19 - Call with ", "4/10 - Call scheduled for 10a on ", "4/9 - Wants to meet with Jay on "]
在这里检查结果
它返回以下匹配项:
(\d{1,2}\/\d{1,2}.+?(?:(?=\d{1,2}\/\d{1,2} - )|\z))
重要部分包括:
它以“日期”开头,然后是其他任何内容(注意最后一个?
,它使+
或*
成为“非贪婪的”)
直到下一个“日期后加破折号”或行尾(这个或很重要,否则你将无法获得最后一场比赛)
该?:
用于忽略结果上的组
(?=\d{1,2}\/\d{1,2} - )|\z)
否则,每次比赛都会得到一个空白的第二组
(?:(...))
请尝试此regexp:
["4/19 - Call with ", "4/10 - Call scheduled for 10a on ", "4/9 - Wants to meet with Jay on "]
在这里检查结果
它返回以下匹配项:
(\d{1,2}\/\d{1,2}.+?(?:(?=\d{1,2}\/\d{1,2} - )|\z))
重要部分包括:
它以“日期”开头,然后是其他任何内容(注意最后一个?
,它使+
或*
成为“非贪婪的”)
直到下一个“日期后加破折号”或行尾(这个或很重要,否则你将无法获得最后一场比赛)
该?:
用于忽略结果上的组
(?=\d{1,2}\/\d{1,2} - )|\z)
否则,每次比赛都会得到一个空白的第二组
(?:(...))
看起来这个自然分隔符是\d{1,2}+/\d{1,2}\s?[-:]+
因此,您可以在否定断言中使用它来获取消息的主体
\d{1,2}+/\d{1,2}\s?[-:]+(?:(?!\d{1,2}+/\d{1,2}\s?[-:]+)[\s\s])*
格式化
Match 1
1. 4/28 - Declined
2.
Match 2
1. 4/19 - Call with Bob, Joe and Steve
2.
Match 3
1. 4/10 - Call scheduled for 10a on 4/18
2.
Match 4
1. 4/9 - Wants to meet with Jay on 4/28 at 10:30a
2.
看起来这个自然分隔符是\d{1,2}+/\d{1,2}\s?[-:]+
因此,您可以在否定断言中使用它来获取消息的主体
\d{1,2}+/\d{1,2}\s?[-:]+(?:(?!\d{1,2}+/\d{1,2}\s?[-:]+)[\s\s])*
格式化
Match 1
1. 4/28 - Declined
2.
Match 2
1. 4/19 - Call with Bob, Joe and Steve
2.
Match 3
1. 4/10 - Call scheduled for 10a on 4/18
2.
Match 4
1. 4/9 - Wants to meet with Jay on 4/28 at 10:30a
2.
输出:
string.split(%r{(\d+/\d+ - )}).drop(1).each_slice(2).map(&:join)
输出:
string.split(%r{(\d+/\d+ - )}).drop(1).each_slice(2).map(&:join)
这个(?![\d{1,2}/\d{1,2}]*[]-)
是有问题的。这是为了做什么?这个(?![\d{1,2}/\d{1,2}]*[]-)
是有问题的。那打算做什么?