具有前瞻功能的正则表达式
我似乎无法让这个正则表达式工作。
输入如下。它确实在一行上,但我在每个 \r\n 之后插入了换行符,以便更容易查看,因此不需要检查空格字符。
01-03\r\n
01-04\r\n
TEXTONE\r\n
STOCKHOLM\r\n
350,00\r\n ---- 350,00 should be the last value in the first match
12-29\r\n
01-03\r\n
TEXTTWO\r\n
COPENHAGEN\r\n
10,80\r\n
这可能会在另一场 01-31 和 02-01 中继续,标志着另一场新比赛(这些是日期)。
我希望此输入总共有 2 个匹配项。 我的问题是,我无法弄清楚如何展望并匹配新比赛的开始(以下两个日期),但不将这些日期包含在第一场比赛中。他们应该属于第二场比赛。
这很难解释,但我希望有人能理解我。 这是我到目前为止得到的,但还不够接近:
(.*?)((?<=\\d{2}-\\d{2}))
我想要的匹配是:
1: 01-03\r\n01-04\r\nTEXTONE\r\nSTOCKHOLM\r\n350,00\r\n
2: 12-29\r\n01-03\r\nTEXTTWO\r\nCOPENHAGEN\r\n10,80\r\n
之后我可以轻松地用 \r\n 分隔列。
I can't seem to make this regex work.
The input is as follows. Its really on one row but I have inserted line breaks after each \r\n so that it's easier to see, so no check for space characters are needed.
01-03\r\n
01-04\r\n
TEXTONE\r\n
STOCKHOLM\r\n
350,00\r\n ---- 350,00 should be the last value in the first match
12-29\r\n
01-03\r\n
TEXTTWO\r\n
COPENHAGEN\r\n
10,80\r\n
This could go on with another 01-31 and 02-01, marking another new match (these are dates).
I would like to have a total of 2 matches for this input.
My problem is that I cant figure out how to look ahead and match the starting of a new match (two following dates) but not to include those dates within the first match. They should belong to the second match.
It's hard to explain, but I hope someone will get me.
This is what I got so far but its not even close:
(.*?)((?<=\\d{2}-\\d{2}))
The matches I want are:
1: 01-03\r\n01-04\r\nTEXTONE\r\nSTOCKHOLM\r\n350,00\r\n
2: 12-29\r\n01-03\r\nTEXTTWO\r\nCOPENHAGEN\r\n10,80\r\n
After that I can easily separate the columns with \r\n.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试以下另一种选择:
Rubular
Here's another option for you to try:
Rubular
为什么要做这么多工作?
输出:
Why do so much work?
Output:
这种更明确的模式对您有用吗?
Can this more explicit pattern work to you?