Monday, February 27, 2023
HomeSoftware DevelopmentExtracting Port Quantity from a localhost API Request to a Server utilizing...

Extracting Port Quantity from a localhost API Request to a Server utilizing Common Expressions


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

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.

Strategy: The issue may be solved primarily based 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 method:

Java

import java.io.*;

import java.util.regex.Matcher;

import java.util.regex.Sample;

  

public class GFG {

  

    

    public static void most important(String[] args)

    {

  

        

        

        String str = "http:/"

                     + "/localhost:8082/api/safe";

        System.out.println("Given String is:n" + str);

        System.out.println(

            "Above Given API is operating on Port Quantity:");

        extractPortNumber(str);

    }

  

    

    

    static void extractPortNumber(String str)

    {

  

        

        

        

        

        String strPattern[] = { "[:]{1}[0-9]{4}" };

        for (int i = 0; i < strPattern.size; i++) {

            Sample sample

                = Sample.compile(strPattern[i]);

            Matcher matcher = sample.matcher(str);

            whereas (matcher.discover()) {

                System.out.println(

                    matcher.group().change(":", ""));

            }

        }

    }

}

Output

Given String is:
http://localhost:8082/api/safe
Above Given API is operating on Port Quantity:
8082

Associated Articles:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto