import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^https?:\\/\\/www\\.google((\\..{1,3}){1,2})\\/(search\\?)?(?!flights|maps|advanced_search)(?!.*&tbm=(shop|fin)&).*";
final String string = "#images\n"
+ "https://www.google.com/search?q=finance&hl=en&tbm=isch&sxsrf=ALeKk03-wtxJ6IxBQ1QplyRfSUR5Zq6p5w:1588804525013&source=lnms&sa=X&ved=0ahUKEwjsstKKpqDpAhWIiqQKHeGbDWcQ_AUICygC&biw=2560&bih=1307&dpr=1\n\n"
+ "#books\n"
+ "https://www.google.com/search?q=finance&hl=en&sxsrf=ALeKk020ZKe6S8ZciwePz_FDqQNjlrIyMw:1588804501205&source=lnms&tbm=bks&sa=X&ved=2ahUKEwiuraX_paDpAhUHLBoKHbvWAKYQ_AUoAXoECBgQCQ&biw=2560&bih=1307\n\n"
+ "#all\n"
+ "https://www.google.com/search?q=finance&hl=en&sxsrf=ALeKk02-xP9nE6zHjjatj9fSG3th2_3zzw:1588804479887&source=lnms&sa=X&ved=0ahUKEwi0k5D1paDpAhWJ-qQKHW1XANcQ_AUIDSgA&biw=2560&bih=1307&dpr=1\n\n"
+ "#news\n"
+ "https://www.google.com/search?q=finance&source=lmns&tbm=nws&bih=1307&biw=2560&hl=en&ved=2ahUKEwi2jsnCpaDpAhWF44UKHcvnDusQ_AUoAXoECAEQAQ\n\n"
+ "https://www.google.com/\n"
+ "https://www.google.fr/\n"
+ "https://www.google.cm/\n"
+ "https://www.google.co.uk/\n\n"
+ "####Dont match following\n\n"
+ "#finance\n"
+ "https://www.google.com/search?hl=en&biw=2560&bih=1307&tbm=fin&sxsrf=ALeKk00e_wrvwwyEY7nhVKV8j5IvHPK8rQ%3A1588802138647&ei=WjKzXuX1JofVkwWv-p24Dg&q=finance&oq=finance&gs_l=finance-immersive.3...980.1734.0.1817.7.5.0.0.0.0.208.208.2-1.1.0....0...1.1.64.finance-immersive..6.1.208...81.0.T31TXEhQon8\n\n"
+ "#flights\n"
+ "https://www.google.com/flights?q=testt&bih=1307&biw=2560&hl=en&tbm=flm&sxsrf=ALeKk03tMI1tx59I3Jc-LJfPj3_gEihKyA:1588801776819&source=lnms&sa=X&ved=0ahUKEwiugJrsm6DpAhVNDOwKHRFgCLkQ_AUINSgD#flt=/m/04jpl..2020-05-22*./m/04jpl.2020-05-26;c:GBP;e:1;ls:1w;sd:0;t:h\n\n"
+ "#maps\n"
+ "https://www.google.com/maps?hl=en&biw=2560&bih=1307&sxsrf=ALeKk00e_wrvwwyEY7nhVKV8j5IvHPK8rQ:1588802138647&q=finance&um=1&ie=UTF-8&sa=X&ved=2ahUKEwiY2IGanaDpAhVFCewKHYSIDCwQ_AUoAHoECAEQCA\n\n"
+ "#shopping\n"
+ "https://www.google.com/search?q=testt&source=lmns&tbm=shop&bih=1307&biw=2560&hl=en&ved=2ahUKEwiShpzrm6DpAhUW_hoKHQ61AEcQ_AUoAnoECAEQAg\n\n"
+ "https://www.google.com/advanced_search?q=dictionary&rlz=1C1CHBF_enGB885GB885&sxsrf=ALeKk01ESVELxIjxyR_5vybRgq6D8oDKhg:1594571776832&hl=en";
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