英文:
Regex to match String containing at least one digit and one number with specific length
问题
很抱歉,我无法翻译代码部分。以下是您要翻译的文本:
"Well, there are million of questions with similiar topic - I have every single response in them but with no success
I have set of urls:
https://test-gateway.com/newUserCustomerOrder
https://test-gateway.com/newUserCustomerOrder/5e8f39ee5bd0920012e40a13
https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14
https://test-gateway.com/newUserCustomerOrder/5e8f3a045bd0920012e40a15
https://test-gateway.com/newUserCustomerOrder/5e8fd4755bd0920012e411fd
https://test-gateway.com/newUserCustomerOrder/5e8fd4e35bd0920012e4120b
https://test-gateway.com/newUserCustomerOrder/5e8fd4eb5bd0920012e4120d
I would like to find orderIds
(they will appear in random place inside of this url for particular scenarios)
regex like:
(?=.*\d)[a-zA-Z\d]{18,24}
Unfortunately catch both id 5e8f39ee5bd0920012e40a13
and adapter name newUserCustomerOrder
I would like to be able to catch each string with length in range {18,24}
which contains at least one digit.
Could anyone give me a hint how to make it work?"
英文:
Well, there are million of questions with similiar topic - I have every single response in them but with no success
I have set of urls:
https://test-gateway.com/newUserCustomerOrder
https://test-gateway.com/newUserCustomerOrder/5e8f39ee5bd0920012e40a13
https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14
https://test-gateway.com/newUserCustomerOrder/5e8f3a045bd0920012e40a15
https://test-gateway.com/newUserCustomerOrder/5e8fd4755bd0920012e411fd
https://test-gateway.com/newUserCustomerOrder/5e8fd4e35bd0920012e4120b
https://test-gateway.com/newUserCustomerOrder/5e8fd4eb5bd0920012e4120d
I would like to find orderIds
(they will appear in random place inside of this url for particular scenarios)
regex like:
(?=.*\d)[a-zA-Z\d]{18,24}
Unfortunately catch both id 5e8f39ee5bd0920012e40a13
and adapter name newUserCustomerOrder
I would like to be able to catch each string with length in range {18,24}
which contains at least one digit.
Could anyone give me a hint how to make it work?
答案1
得分: 2
尝试使用以下正则表达式模式:
\b(?=[^/]*\d)\w{18,24}\b
【演示】:
正则表达式解释:
\b
:匹配单词边界(很可能是 /)(?=[^/]*\d)
:然后断言在 URL 的下一个部分中有一个数字,但不通过 /\w{18,24}
:匹配 18 到 24 个单词字符\b
:再次匹配单词边界
以下是在 Java 中使用此正则表达式的一行代码:
String url = "https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14";
String orderId = url.replaceAll(".*\\b((?=[^/]*\\d)\\w{18,24})\\b.*", "$1");
System.out.println(orderId);
这将打印:
5e8f39f65bd0920012e40a14
英文:
Try using the following regex pattern:
\b(?=[^/]*\d)\w{18,24}\b
Explanation of regex:
\b match a word boundary (most likely /)
(?=[^/]*\d) then assert that a digit follows in the next segment of the URL,
without passing /
\w{18,24} match 18 to 24 word characters
\b match another word boundary
Here is a one-liner in Java using this regex:
String url = "https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14";
String orderId = url.replaceAll(".*\\b((?=[^/]*\\d)\\w{18,24})\\b.*", "$1");
System.out.println(orderId);
This prints:
5e8f39f65bd0920012e40a14
答案2
得分: 2
不是要预测 .*\d
,而是要预测单词字符后跟\d
,从而排除 newUserCustomerOrder/
,因为它包含斜杠:
(?=\w*\d)[a-zA-Z\d]{18,24}
链接:https://regex101.com/r/GLrhNd/1
英文:
Rather than looking ahead for .*\d
, lookahead for word characters followed by \d
, thus excluding the newUserCustomerOrder/
since it contains a slash:
(?=\w*\d)[a-zA-Z\d]{18,24}
答案3
得分: 2
你可以使用这个正则表达式:
(?=[^/]*\d)([a-zA-Z\d]{18,24})
(?=[^/]*\d)
是正向先行断言,用于断定在0个或更多非/
字符后面存在数字。
英文:
You may use this regex:
(?=[^/]*\d)([a-zA-Z\d]{18,24})
(?=[^/]*\d)
is positive lookahead to assert presence of digit following 0 or more non-/
characters.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论