import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "https?://\\S+?cnbc\\.com\\S+";
final String string = "\n\n"
+ "I have a text file of links after scrapping, I need to make a regular expression for these links so i can extract them from a file, but different links have same structure but different in length, like\n\n"
+ "https://www.cnbc.com/2016/10/12/billionaire-richard-branson-learned-a-key-business-lesson-playing-tennis.html\n\n"
+ "and this:\n\n"
+ "https://www.cnbc.com/2016/10/12/hedge-fund-bonus-makeover.html\n\n"
+ "I can successfully make RE for the base domain, but after that title give me a tough time, mine is\n\n"
+ "[h][t][t][p][s]:\\/\\/[w][w][w].[c][n][b][c].[c][o][m]\\/[2][0][1][5-8] \n\n"
+ "for https://www.cnbc.com/2016/10/11/ but dont know how to make for further with diiferent words for different links ahead,\n";
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