英文:
group string after keywords (at, after, until) ignore order
问题
Sure, here are the translated parts:
-
I want to group any text after one of these keyword for
:
我想要在这些关键词之后对任何文本进行分组 -
input: /send 1 at 11:00pm for 3min
:
输入:/发送 1 在 11:00pm 时持续 3 分钟 -
desired result: postNumber = 1, sendAt = 11:00pm, duration = 3min
:
期望结果:帖子编号 = 1,发送时间 = 11:00pm,持续时间 = 3 分钟 -
input: /send 1 for 3min
:
输入:/发送 1 持续 3 分钟 -
desired result: postNumber = 1, duration = 3min
:
期望结果:帖子编号 = 1,持续时间 = 3 分钟 -
input: /send 1 at 11:00pm
:
输入:/发送 1 在 11:00pm 时 -
desired result: postNumber = 1, sendAt = 11:00pm
:
期望结果:帖子编号 = 1,发送时间 = 11:00pm -
input: /send 1 until 11:00pm
:
输入:/发送 1 直到 11:00pm 时 -
desired result: postNumber = 1, until = 11:00pm
:
期望结果:帖子编号 = 1,直到时间 = 11:00pm -
input: /send 1 for 3min at 11:00 pm
:
输入:/发送 1 持续 3 分钟 在 11:00 pm 时 -
desired result: postNumber = 1, sendAt = 11:00 pm, duration = 3min
:
期望结果:帖子编号 = 1,发送时间 = 11:00 pm,持续时间 = 3 分钟 -
input: /send 1 at 11am for 1 h
:
输入:/发送 1 在 11am 时 持续 1 小时 -
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+))?)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论