Java 正则表达式在输出中缺少匹配项
我目前正在与正则表达式相匹配。我的模式是:
"(?<=\p{Alnum}|\p{Punct})(\p{Alnum}+\p{Punct}{1})"
我将其与字符串匹配:
"https://www.google.com/"
我所需的结果与上述正则弦和字符串是:
https:, www., google., com/
我能够成功获得所有匹配项,但除了'https:'一个。在这种情况下,它给出了“ ttps:”而不是所需的'https:'
我无法理解我在哪里出错。有人可以帮我解决这个问题吗?
I am currently matching a string against a regular expression. My pattern is:
"(?<=\p{Alnum}|\p{Punct})(\p{Alnum}+\p{Punct}{1})"
I am matching it with the string:
"https://www.google.com/"
My desired result with the above regex and string is:
https:, www., google., com/
I am able to get all the matches successfully except 'https:' one. In that case it is giving out 'ttps:' instead of the required 'https:'
I am not able to understand where I went wrong. Can anyone please help me in figuring this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
在线regex demo 。
(?字母数字和标点符号。
请注意,您的正则需要立即在左侧的字母数字或标点符符,因此不可能匹配字符串位置的开始。
请注意,
{1}
始终是多余的,您可以在”写清洁器的正则表达式“我的视频。You can use
See the online regex demo.
The
(?<![^\p{Alnum}\p{Punct}])
negative lookbehind matches a location that is not immediately preceded by a char other than an alphanumeric and a punctuation char.Note that your regex required an alphanumeric or punctuation char immediately on the left, so it was impossible to match the start of string position.
Note that
{1}
is always redundant, you can see more about regex redundancy in the "Writing cleaner regular expressions" YT video of mine.