英文:
Having problems with java regex
问题
我有以下的正则表达式:
/[-A-Z]{4}\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}.png
基本上,我想检查基本类型的字符串:
ABCD12/<这里是一个随机的 UUID 作为字符串>.png
UUID(大写)的检查正常工作,但现在让我们看一个特殊情况。我想要接受以下类型的字符串:
--CD12/...
AB--12/...
但不想接受这样的:
A--D12/...
但是我无法得到正则表达式的第一部分正确。基本上,我需要检查每个两个数字或两个 - 重复两次。
根据我的理解,[-A-Z]{4} 意味着 "要么是 -,要么是 A 到 Z 之间的字符,长度为 4"。所以为什么我的模式不起作用?
编辑:
在评论中发布了这个答案,它可以工作:
(?mi)^(?:--[A-Z]{2}|[A-Z]{2}(?:--|[A-Z]{2}))\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\.png$
有人能解释一下 (?mi) 和 (?:...) 是什么意思吗?正常的 ? 表示出现 0 次或 1 次,但 : 有什么作用?
编辑 2:
只是为了那些可能有类似问题但不想阅读所有这些正则表达式的人
我稍微修改了一个答案,以便接受像 ----12 这样的模式。最终结果是:
"^/(?:--[A-Z]{2}|-{4}|[A-Z]{2}(?:--|[A-Z]{2}))\\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\\.png$"
它运行得非常好。
英文:
I have the following regex:
/[-A-Z]{4}\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}.png
Basically I want to check for strings of the basic type
ABCD12/<here_is_a_random_uuid_as_a_string>.png
The UUID (which is in UPPER CASE) checking works fine, but now let's take a look at a special case. I want to accept strings like this
--CD12/...
AB--12/...
but NOT like this:
A--D12/...
But I can not get the first part of the regex right. Basically I need to check for either two digits or two -after each other twice.
For my understanding [-A-Z]{4} means "either - or something between A - Z with a length of 4". So why doesn't my pattern work?
EDIT:
This answer was posted within the comments and it works:
(?mi)^(?:--[A-Z]{2}|[A-Z]{2}(?:--|[A-Z]{2}))\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\.png$
Can somebody explain to me what (?mi) and what (?:...) means? The normal ? means 0 or 1 time, but what is the : for?
EDIT 2:
Just for those how might have a similar problem and do not want to read all of those regexes ![]()
I slightly modified an answer to also accept patterns like ----12. The end result:
"^/(?:--[A-Z]{2}|-{4}|[A-Z]{2}(?:--|[A-Z]{2}))\\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\\.png$"
It works like a charm.
答案1
得分: 2
你可以在你的情况下使用这个正则表达式:
^(?:--[A-Z]{2}|[A-Z]{2}(?:--|[A-Z]{2}))\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\.png$
有关第一部分的详细信息:
^:开头(?::开始非捕获组--[A-Z]{2}:匹配--后面跟着 2 个字母|:或者[A-Z]{2}:匹配 2 个字母(?:--|[A-Z]{2}):匹配--或者 2 个字母
):结束非捕获组
顺便说一下,(?:...) 是非捕获组。
英文:
You may use this regex for your cases:
^(?:--[A-Z]{2}|[A-Z]{2}(?:--|[A-Z]{2}))\d{2}/[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}\.png$
Details about first part:
^: Start(?:: Start non-capture group--[A-Z]{2}: Match--followed by 2 letters|: OR[A-Z]{2}: Match 2 letters(?:--|[A-Z]{2}): Match--OR 2 letters
): End non-capture group
btw (?:...) is non-capture group.
答案2
得分: 1
你的 [-A-Z]{4} 匹配任意四个大写 ASCII 字母或 -,因此它也可以匹配 ----、A---、---B、-B-- 等。
你想确保如果有连字符,它们出现在两个字母之前或之后:
(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})
它的意思是:
(?:- 开始一个非捕获组:[A-Z]{2}--- 两个大写 ASCII 字母,然后是--|- 或者--[A-Z]{2}---,然后是任意两个大写 ASCII 字母|- 或者[A-Z]{4}- 任意四个大写 ASCII 字母
)- 结束非捕获组。
完整的模式:
(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\.png
为了强制整个字符串匹配,添加 ^(字符串开头)和 $(字符串结尾)锚点:
^(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\.png$
注意,. 匹配任意字符,如果要匹配字面上的句点,应该对其进行转义。
英文:
Your [-A-Z]{4} matches any four occurrences of an uppercase ASCII letter or -, so it can also match ----, A---, ---B, -B--, etc.
You want to make sure that if there are hyphens, they come after or before two letters:
(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})
It means:
(?:- start of a non-capturing group:[A-Z]{2}--- two uppercase ASCII letters and then--|- or--[A-Z]{2}---and then any two uppercase ASCII letters|- or[A-Z]{4}- any four uppercase ASCII letters
)- end of the non-capturing group.
The full pattern:
(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\.png
To force the entire string match, add ^ (start of string) and $ (end of string) anchors:
^(?:[A-Z]{2}--|--[A-Z]{2}|[A-Z]{4})\d{2}/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\.png$
See the regex demo
Note the . matches any char, to match a literal dot, you should escape it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论