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?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
ganjan
  • 7,356
  • 24
  • 82
  • 133
  • 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? – Roger Lindsjö Nov 20 '11 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. – DPM Nov 20 '11 at 20:10
  • you may try this : http://stackoverflow.com/a/25866412/3767784 – FaNaJ Sep 16 '14 at 10:34

2 Answers2

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
Marcus
  • 12,296
  • 5
  • 48
  • 66
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.

Mob
  • 10,958
  • 6
  • 41
  • 58