Reputation: 7576
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?
Upvotes: 3
Views: 6145
Reputation: 11098
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.
Upvotes: 2
Reputation: 12586
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
Upvotes: 7