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

huangapple go评论64阅读模式
英文:

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

<h2>Demo</h2>

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 = &quot;https://test-gateway.com/newUserCustomerOrder/5e8f39f65bd0920012e40a14&quot;;
String orderId = url.replaceAll(&quot;.*\\b((?=[^/]*\\d)\\w{18,24})\\b.*&quot;, &quot;$1&quot;);
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}

https://regex101.com/r/GLrhNd/1

答案3

得分: 2

你可以使用这个正则表达式:

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

正则表达式演示链接

(?=[^/]*\d) 是正向先行断言,用于断定在0个或更多非/字符后面存在数字。

英文:

You may use this regex:

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

RegEx Demo

(?=[^/]*\d) is positive lookahead to assert presence of digit following 0 or more non-/ characters.

huangapple
  • 本文由 发表于 2020年4月10日 15:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定