Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式匹配至少包含一个数字和一个具有特定长度的数字的字符串_Java_Regex - Fatal编程技术网

Java 正则表达式匹配至少包含一个数字和一个具有特定长度的数字的字符串

Java 正则表达式匹配至少包含一个数字和一个具有特定长度的数字的字符串,java,regex,Java,Regex,好吧,有数百万个主题类似的问题-我有每一个答案,但没有成功: 我有一组URL: https://test-gateway.com/newUserCustomerOrder https://test-gateway.com/newUserCustomerOrder/5e8f39ee5bd0920012e40a13 https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14 https://test-gateway.c

好吧,有数百万个主题类似的问题-我有每一个答案,但没有成功:

我有一组URL:

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
我想找到OrderID,它们将出现在特定场景下此url内的随机位置 类正则表达式:

?=.*\d[a-zA-Z\d]{18,24}

不幸的是,捕获了id 5E8F39EE5BD090012E40A13和适配器名称newUserCustomerOrder 我希望能够捕获长度在{18,24}范围内的每个字符串,其中至少包含一个数字


有人能给我一个如何使它工作的提示吗?

尝试使用以下正则表达式模式:

\b(?=[^/]*\d)\w{18,24}\b
正则表达式的解释:

\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
下面是使用此正则表达式的Java单行程序:

String url = "https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14";
String orderId = url.replaceAll(".*\\b((?=[^/]*\\d)\\w{18,24})\\b.*", "$1");
System.out.println(orderId);
(?=[^/]*\d)([a-zA-Z\d]{18,24})
这张照片是:

5e8f39f65bd0920012e40a14

与其向前看。*\d,不如向前看紧跟着\d的单词字符,这样就排除了newUserCustomerOrder/,因为它包含斜杠:

(?=\w*\d)[a-zA-Z\d]{18,24}

您可以使用此正则表达式:

String url = "https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14";
String orderId = url.replaceAll(".*\\b((?=[^/]*\\d)\\w{18,24})\\b.*", "$1");
System.out.println(orderId);
(?=[^/]*\d)([a-zA-Z\d]{18,24})
?=[^/]*\d为正向前瞻,以断言0个或更多非-/字符后是否存在数字。

使用?,