英文:
Fails to match when using backreference
问题
I want to match<Br>
9:00 AM to 5:00 PM
<br>
This is my regex, I am trying to use backreference match the time at the end, it is not working
<br>(\d(?::\d{2})?\s\wM)\sto\s\1
If simply replacing the backreference makes it match, why is the backreference failing ?<br>
(\d(?::\d{2})?\s\wM)\sto\s(\d(?::\d{2})?\s\wM)
My questions are,<br>
How can I use a backreference to match the time at both ends ?<br>
Why is what I did not working ?
英文:
I want to match<Br>
9:00 AM to 5:00 PM
<br>
This is my regex, I am trying to use backreference match the time at the end, it is not working
<br>(\d(?::\d{2})?\s\wM)\sto\s\1
If simply replacing the backreference makes it match, why is the backreference failing ?<br>
(\d(?::\d{2})?\s\wM)\sto\s(\d(?::\d{2})?\s\wM)
My questions are,<br>
How can I use a backreference to match the time at both ends ?
<br>Why is what I did not working ?
答案1
得分: 1
这部分的中文翻译如下:
我认为对于 "\1" 的理解存在误区。它不会重新使用模式,就好像它是括号内表达式的占位符一样。相反,它匹配了第一个捕获组中捕获到的内容。
在你的情况下,你的模式会匹配例如这样的内容:
- 9:00 AM to 9:00 AM
请参考这里:https://regex101.com/r/M8u6eO/1
如果你的正则表达式引擎支持,你可以使用 ""?1""。
(\d:\d{2}?\s\wM)\sto\s(?1)
请参考这里:https://regex101.com/r/OytMlt/1
这将确实重新使用模式而不是匹配。
英文:
I think there is a misconception of what \1 does. It does not re-use the pattern, as if it was a placeholder for the expression you put in parenthesis. Rather, it matches whatever was captured in the first capturing group.
In your case, your pattern would match e.g. this:
- 9:00 AM to 9:00 AM
See here: https://regex101.com/r/M8u6eO/1
If your regex engine supports it, you may use "?1".
(\d:\d{2}?\s\wM)\sto\s(?1)
See here: https://regex101.com/r/OytMlt/1
This will indeed re-use the pattern instead of the match.
答案2
得分: 1
The backreference is not working because it is capturing the whole time string, including the "AM" or "PM" part. Since the two time strings have different "AM" or "PM" parts, the backreference does not match.
To fix the regex and use a backreference, you can capture only the time part (hour and minute) and use separate backreferences for both the hour and minute parts. Here's a working regex:
(\d(?::(\d{2}))?\s\wM)\sto\s
Regex breakdown:
- (\d(?::(\d{2}))?\s\wM): 仅捕获时间部分,包括 "AM" 或 "PM" 部分。
- \d: 捕获小时部分。
- (?::(\d{2}))?: 可选地捕获分钟部分,包括冒号。
- \s\wM: 匹配 "AM" 或 "PM" 部分。
- \sto\s: 匹配两个时间字符串之间的 " to "。
- \1: 回溯到第一个捕获的组(整个时间字符串)。
Note that with this regex, you're not validating if the first part is "AM" and the second part is "PM". If you want to ensure this, you can modify the regex like this:
(\d(?::(\d{2}))?\sAM)\sto\s(\d(?::\d{2})?\sPM)
这个正则表达式确保第一部分以 "AM" 结尾,第二部分以 "PM" 结尾。
英文:
The backreference is not working because it is capturing the whole time string, including the "AM" or "PM" part. Since the two time strings have different "AM" or "PM" parts, the backreference does not match.
To fix the regex and use a backreference, you can capture only the time part (hour and minute) and use separate backreferences for both the hour and minute parts. Here's a working regex:
(\d(?::(\d{2}))?\s\wM)\sto\s
Regex breakdown:
- (\d(?::(\d{2}))?\s\wM): Capture the whole time string, including the "AM" or "PM" part.
- \d: Capture the hour part.
- (?::(\d{2}))?: Optionally capture the minute part, including the colon.
- \s\wM: Match the "AM" or "PM" part.
- \sto\s: Match " to " in between the two time strings.
- \1: Backreference to the first captured group (the whole time string).
Note that with this regex, you're not validating if the first part is "AM" and the second part is "PM". If you want to ensure this, you can modify the regex like this:
(\d(?::(\d{2}))?\sAM)\sto\s(\d(?::\d{2})?\sPM)
This regex ensures that the first part ends with "AM" and the second part ends with "PM".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论