Java 正则表达式在输出中缺少匹配项

发布于 2025-01-18 00:21:03 字数 357 浏览 2 评论 0原文

我目前正在与正则表达式相匹配。我的模式是:

"(?<=\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 技术交流群。

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

发布评论

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

评论(1

一瞬间的火花 2025-01-25 00:21:03

您可以使用

(?<![^\p{Alnum}\p{Punct}])(\p{Alnum}+\p{Punct})

在线regex demo

(?字母数字和标点符号。

请注意,您的正则需要立即在左侧的字母数字或标点符符,因此不可能匹配字符串位置的开始。

请注意,{1}始终是多余的,您可以在”写清洁器的正则表达式“我的视频

You can use

(?<![^\p{Alnum}\p{Punct}])(\p{Alnum}+\p{Punct})

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.

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