Java正则表达式
1. 不包含thumb.jpg,但包含_HH_,并以.jpg结尾
public static void testMatch3() { // https://www.runoob.com/java/java-regular-expressions.html String content1 = "GF3_KAS_FSII_021168_E105.2_N32.5_20200817_L1A_HH_L10004997197.jpg"; String content2 = "GF3_KAS_FSII_021168_E105.2_N32.5_20200817_L1A_HH_L10004997197.thumb.jpg"; // String content3 = "GF3_KAS_FSII_021168_E105.2_N32.5_20200817_L1A_HV_L10004997197.jpg"; String content4 = "GF3_KAS_FSII_021168_E105.2_N32.5_20200817_L1A_HV_L10004997197.thumb.jpg"; // ^.*-mss2.jpg$;^.*-pan2.jpg$ String pattern = "^((?!thumb).)*_HH_*((?!thumb).)*(.jpg$)"; // "^((?!thumb).)*$"; // ^(?!.*?robots).*$ boolean isMatch1 = Pattern.matches(pattern, content1); // true boolean isMatch2 = Pattern.matches(pattern, content2); // false boolean isMatch3 = Pattern.matches(pattern, content3); // true boolean isMatch4 = Pattern.matches(pattern, content4); // false System.out.println( "---------testMatch3-------是否包含了字符串: " + isMatch1 + " | " + isMatch2 + " | " + isMatch3 + " | " + isMatch4); }
2. 不包含_Check.xml,但以.xml结尾
public static void testMatch4() { // https://www.runoob.com/java/java-regular-expressions.html String content1 = "GF5_AHSI_E105.64_N32.80_20200117_009006_L10000071673.xml"; String content2 = "GF5_AHSI_E105.64_N32.80_20200117_009006_L10000071673_Check.xml"; // ^.*-mss2.jpg$;^.*-pan2.jpg$ String pattern = "^((?!_Check).)*(.xml$)"; // "^((?!thumb).)*$"; // ^(?!.*?robots).*$ boolean isMatch1 = Pattern.matches(pattern, content1); // true boolean isMatch2 = Pattern.matches(pattern, content2); // false System.out.println( "---------testMatch3-------是否包含了字符串: " + isMatch1 + " | " + isMatch2 ); }
-----------------------