Given a String test_str as localhost API Request Handle, the duty is to get the extract port variety of the service.
Examples:
Enter: test_str = ‘http://localhost:8109/customers/addUsers’
Output: 8109
Rationalization: Port Quantity, 8109 extracted.Enter: test_str = ‘http://localhost:1337/api/merchandise’
Output: 1337
Rationalization: Port Quantity, 1337 extracted.
Method: The issue could be solved based mostly on the next concept:
Create a regex sample to validate the quantity as written beneath:
regex = “[:]{1}[0-9]{4, }“The place,
- [:]: This sample will match if one of many previous is a colon character
- [0-9]{4} : This sample will enable 4 or greater than 4 previous components if they’re digits.
Observe the beneath steps to implement the thought:
- Create a regex expression to extract all of the Port numbers from the string.
- Use Sample class to compile the regex fashioned.
- Use the matcher perform to search out.
Beneath is the code implementation of the above-discussed strategy:
Java
|
Given String is: http://localhost:8082/api/safe Above Given API is working on Port Quantity: 8082
Associated Articles: