关键词后的字符串(在、之后、直到)忽略顺序

huangapple go评论106阅读模式
英文:

group string after keywords (at, after, until) ignore order

问题

Sure, here are the translated parts:

  1. I want to group any text after one of these keyword for:
    我想要在这些关键词之后对任何文本进行分组

  2. input: /send 1 at 11:00pm for 3min:
    输入:/发送 1 在 11:00pm 时持续 3 分钟

  3. desired result: postNumber = 1, sendAt = 11:00pm, duration = 3min:
    期望结果:帖子编号 = 1,发送时间 = 11:00pm,持续时间 = 3 分钟

  4. input: /send 1 for 3min:
    输入:/发送 1 持续 3 分钟

  5. desired result: postNumber = 1, duration = 3min:
    期望结果:帖子编号 = 1,持续时间 = 3 分钟

  6. input: /send 1 at 11:00pm:
    输入:/发送 1 在 11:00pm 时

  7. desired result: postNumber = 1, sendAt = 11:00pm:
    期望结果:帖子编号 = 1,发送时间 = 11:00pm

  8. input: /send 1 until 11:00pm:
    输入:/发送 1 直到 11:00pm 时

  9. desired result: postNumber = 1, until = 11:00pm:
    期望结果:帖子编号 = 1,直到时间 = 11:00pm

  10. input: /send 1 for 3min at 11:00 pm:
    输入:/发送 1 持续 3 分钟 在 11:00 pm 时

  11. desired result: postNumber = 1, sendAt = 11:00 pm, duration = 3min:
    期望结果:帖子编号 = 1,发送时间 = 11:00 pm,持续时间 = 3 分钟

  12. input: /send 1 at 11am for 1 h:
    输入:/发送 1 在 11am 时 持续 1 小时

  13. desired result: postNumber = 1, sendAt = 11am, duration = 1 h:
    期望结果:帖子编号 = 1,发送时间 = 11am,持续时间 = 1 小时

I've provided translations for the text you requested. If you need further assistance or have any specific questions, please feel free to ask.

英文:

I want to group any text after one of these keyword for

input: /send 1 at 11:00pm for 3min

desired result: postNumber = 1, sendAt = 11:00pm, duration = 3min

input: /send 1 for 3min

desired result: postNumber = 1, duration = 3min

input: /send 1 at 11:00pm

desired result: postNumber = 1, sendAt = 11:00pm

input: /send 1 until 11:00pm

desired result: postNumber = 1, until = 11:00pm

input: /send 1 for 3min at 11:00 pm

desired result: postNumber = 1, sendAt = 11:00 pm, duration = 3min

input: /send 1 at 11am for 1 h

desired result: postNumber = 1, sendAt = 11am, duration = 1 h

I've made the following regex but could'nt figure out how to make it agnostic to order of the keywords input

(?<postNumber>\d+)(?: at (?<sendAt>.*))?(?: for (?<duration>.*))?(?: until (?<until>.*))?

I tried to add positive lockahead but it matched all the digits in the first group
(?<postNumber>\d+)(?=(?: at (?<timeToSend>.*)))?(?=(?: for (?<duration>.*)))?(?=(?: until (?<until>.*)))?

答案1

得分: 4

你可以在每个捕获组周围加上正向预查模式,以便可以以任何顺序匹配它们:

\/send\s+(?<postNumber>\d+)(?=(?:.*\bat\s+(?<sendAt>\d+(?::\d+)?\s*\S+))?)(?=(?:.*\bfor\s+(?<duration>\d+\s*\S+))?)(?=(?:.*\buntil\s+(?<until>\d+(?::\d+)?\s*\S+))?)

示例:https://regex101.com/r/Ks5YHN/2

英文:

You can enclose each of the capture groups in a positive lookahead pattern so that they can be matched in any order:

\/send\s+(?<postNumber>\d+)(?=(?:.*\bat\s+(?<sendAt>\d+(?::\d+)?\s*\S+))?)(?=(?:.*\bfor\s+(?<duration>\d+\s*\S+))?)(?=(?:.*\buntil\s+(?<until>\d+(?::\d+)?\s*\S+))?)

Demo: https://regex101.com/r/Ks5YHN/2

huangapple
  • 本文由 发表于 2023年6月12日 09:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定