Reputation: 2353
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?
Upvotes: 0
Views: 170
Reputation: 522762
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
Upvotes: 2
Reputation: 786291
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.
Upvotes: 2
Reputation: 371193
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
Upvotes: 2