具有前瞻功能的正则表达式

发布于 2024-10-16 03:00:39 字数 719 浏览 1 评论 0原文

我似乎无法让这个正则表达式工作。

输入如下。它确实在一行上,但我在每个 \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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜`诱少女 2024-10-23 03:00:40

您可以尝试以下另一种选择:

(.+?)(?=\d{2}-\d{2}\\r\\n\d{2}-\d{2}|$)

Rubular

Here's another option for you to try:

(.+?)(?=\d{2}-\d{2}\\r\\n\d{2}-\d{2}|$)

Rubular

面如桃花 2024-10-23 03:00:40
/
   \G
   (
      (?:
         [0-9]{2}-[0-9]{2}\r\n
      ){2}
      (?:
         (?! [0-9]{2}-[0-9]{2}\r\n ) [^\n]*\n
      )*
   )
/xg
/
   \G
   (
      (?:
         [0-9]{2}-[0-9]{2}\r\n
      ){2}
      (?:
         (?! [0-9]{2}-[0-9]{2}\r\n ) [^\n]*\n
      )*
   )
/xg
美胚控场 2024-10-23 03:00:40

为什么要做这么多工作?

$string = q(01-03\r\n01-04\r\nTEXTONE\r\nSTOCKHOLM\r\n350,00\r\n12-29\r\n01-03\r\nTEXTTWO\r\nCOPENHAGEN\r\n10,80\r\n);
for (split /(?=(?:\d{2}-\d{2}\\r\\n){2})/, $string) {
   print join( "\t", split /\\r\\n/), "\n"
}

输出:

01-03   01-04   TEXTONE STOCKHOLM       350,00
12-29   01-03   TEXTTWO COPENHAGEN      10,80`

Why do so much work?

$string = q(01-03\r\n01-04\r\nTEXTONE\r\nSTOCKHOLM\r\n350,00\r\n12-29\r\n01-03\r\nTEXTTWO\r\nCOPENHAGEN\r\n10,80\r\n);
for (split /(?=(?:\d{2}-\d{2}\\r\\n){2})/, $string) {
   print join( "\t", split /\\r\\n/), "\n"
}

Output:

01-03   01-04   TEXTONE STOCKHOLM       350,00
12-29   01-03   TEXTTWO COPENHAGEN      10,80`
泡沫很甜 2024-10-23 03:00:39

这种更明确的模式对您有用吗?

(\d{2}-\d{2})\r\n(\d{2}-\d{2})\r\n(.*)\r\n(.*)\r\n(\d+(?:,?\d+))

Can this more explicit pattern work to you?

(\d{2}-\d{2})\r\n(\d{2}-\d{2})\r\n(.*)\r\n(.*)\r\n(\d+(?:,?\d+))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文