3

I have a text file, and loop though the file like this:

for ( int i = 0; i < this.textLines.size(); i++ ) {
    String tempString = textLines.get( i );

So now I have tempString containing something like:

46.102.241.199:3128 0.2990 Transp. NN N 100% 2011-11-19 17:56:02

What I want to to is return the IP:PORT part, in this case: 46.102.241.199:3128

How can I do that?

3
  • You say "something like", will all lines have the same format or will some not contain any IP:PORT? Will some have different data before IP:PORT? Commented Nov 20, 2011 at 20:05
  • Your tags suggest you want to use regular expression, however why use a more complicated solution when a simpler one might do? Can't you just do tempString.substring(0, tempString.indexOf(' '));? Of course you are not checking the correct format of the string in this way, but do you need to? I would aim for the simplest that gets the job done. Commented Nov 20, 2011 at 20:10
  • you may try this : stackoverflow.com/a/25866412/3767784 Commented Sep 16, 2014 at 10:34

2 Answers 2

7

This regex would get you an IP with an optional port. If there's always a port remove the questionmark at the end of the line.

\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})?

Note that this is a simplified validation of an IPv4 and will only match that they are one the correct format and not a valid one. And remember to add an extra backslash to escape each backslash in java.

Here's an example in java:

String text = "46.102.241.199:3128 0.2990 Transp. NN N 100% 2011-11-19 17:56:02";
String pattern = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})?";

Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(text);
while (matcher.find()) {
    System.out.println(matcher.group());
}

Output:

46.102.241.199:3128
Sign up to request clarification or add additional context in comments.

Comments

2

I don't think you need regex for this, this is possible with StringTokenizer.

import java.util.ArrayList;
import java.util.StringTokenizer;

public class SOScrap{
public static void main(String[] args) {
    ArrayList<String> as = new ArrayList<String>();
    ArrayList<String> asa = new ArrayList<String>();
    String s = "46.102.241.199:3128 0.2990 Transp. NN N 100% 2011-11-19 17:56:02";
    StringTokenizer st = new StringTokenizer(s, " ");
        while(st.hasMoreTokens()){
              as.add(st.nextToken());
        }

    StringTokenizer astk = new StringTokenizer(as.get(0), ":");

        while(astk.hasMoreTokens()){
           asa.add(astk.nextToken());
        }
    System.out.println(asa);
}

}

Outputs

[46.102.241.199, 3128]

You can now access the elements in an ArrayList. The first index holds the IP while the second holds the port.

Comments

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.