import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(\\S+\\s+){3}(?<originating>\\S+)\\s+(?<exchange>\\S+)";
final String string = "#000#000#000031419 102900107 8*902 17145550104 71234 88888 0 0244 71247 0 0000101 #015\n"
+ "#000#000#000031419 132500329 #060 713312155555331 50042424 25331 0 0000316 #015";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html