0

Is there any way that you can remove IPs from a string?

The input file we get sometimes has IPs attached to the user name, e.g.

Jan vd Merwe
Piet Breedt (172.166.23.41)
Jan vd Merwe (164.23.23.51)
Sarel Fourie (23.12.167.244)
Piet Breedt

So if there is an IP, I want to remove it. I have been looking at alot of functions but cant seem to get the indexing and parameters right.

2
  • Do you like to extract the ip? (Or remove it and extract the name) Commented Nov 14, 2010 at 15:39
  • Do you want to remove it in the file? Commented Nov 15, 2010 at 23:34

2 Answers 2

2

You could look for the ( and remove the remaining string.

public class Main {

    public static void main(String[] args) {

        String[] names = { "Jan vd Merwe", 
                           "Piet Breedt (172.166.23.41)",
                           "Jan vd Merwe (164.23.23.51)", 
                           "Sarel Fourie (23.12.167.244)" };

        for (String name : names) {

            int parIndex = name.indexLastOf('(');
            if (parIndex != -1)
                name = name.substring(0, parIndex-1);

            System.out.println(name);
        }
    }
}

prints:

Jan vd Merwe
Piet Breedt
Jan vd Merwe
Sarel Fourie

Another solution, based on regular expressions:

    String[] names = { "Jan vd Merwe", 
                       "Piet Breedt (172.166.23.41)",
                       "Jan vd Merwe (164.23.23.51)", 
                       "Sarel Fourie (23.12.167.244)" };

    String ipExp = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
    Pattern pattern = Pattern.compile("(.*?)\\s*(\\(" + ipExp + "\\))?");

    for (String nameIp : names) {
        Matcher m = pattern.matcher(nameIp);
        if (m.matches()) {
            String name = m.group(1);
            String ip = m.group(2) == null ? "n/a" : m.group(2);
            System.out.printf("Name: %s, Ip: %s%n", name, ip);
        }
    }

Prints

Name: Jan vd Merwe, Ip: n/a
Name: Piet Breedt, Ip: (172.166.23.41)
Name: Jan vd Merwe, Ip: (164.23.23.51)
Name: Sarel Fourie, Ip: (23.12.167.244)
Sign up to request clarification or add additional context in comments.

4 Comments

your first solution will fail for Jan vd Merwe(Jan) such data
That's not in the 'spec'. It says IPs. Maybe the OP wants to keep the stuff in the parenthesis if it isn't an IP.
@org.life.java: That wasn't defined anywhere. No need to worry about special cases if OP is sure there aren't any.
@org.life.java, changed to lastIndexOf... :-)
1

Use String.replace():

String myString = "Piet Breedt (172.166.23.41)";
// replace everything in between the parens (including the parens) by the empty String
myString = myString.replace("\(.*\)", "");

Of course, the regex could be specialised to only match ips, something like

\(\\d{1,3}\.\\d{1,3}\.\\d{1,3}.\\d{1,3}\)

EDIT

Here's how you would replace the lines in the files (pseudocode):

  1. Open the original file. Open a temporary file.
  2. Read line by line. This will give you a String for every line.
  3. Remove the contents within the String. Write the new String to the temporary file.
  4. When done, replace the original file by the temporary file.

2 Comments

regular expression should be \(\\d{1,3}\.\\d{1,3}\.\\d{1,3}.\\d{1,3}\)
how would i use it without the array, ie what would i need to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.