diff --git a/.idea/modules.xml b/.idea/modules.xml index 50c8b88..78a93a1 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -8,6 +8,7 @@ + diff --git a/Week8/Week8.iml b/Week8/Week8.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week8/Week8.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week8/src/Exercise1.java b/Week8/src/Exercise1.java new file mode 100644 index 0000000..5966b9f --- /dev/null +++ b/Week8/src/Exercise1.java @@ -0,0 +1,50 @@ +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Exercise1 { + public static void main(String[] args) { + String regex = "((add)|(sub)|(max)|(min)|(doubleMe))\\(\\d+(.\\d+)?,\\d+(.\\d+)?\\)"; + Pattern p = Pattern.compile(regex); + + String str = new Scanner(System.in).nextLine(); + String expressionStr = str; + + Matcher matcher = p.matcher(str); + String res = ""; + while (matcher.find()){ + String subStr = matcher.group(); + res = calcExpression(subStr)+""; + str = str.replaceAll(regex,res); + matcher = p.matcher(str); + } + System.out.println(expressionStr + "=" + res); + } + + public static double calcExpression(String expression){ + double res = 0; + String exp = expression.substring(0, expression.indexOf("(")); + if ("doubleMe".equals(exp)){ + double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")"))); + res = num*2; + } else { + double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(","))); + double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")"))); + switch (exp){ + case "add": + res = num1+num2; + break; + case "sub": + res = num1-num2; + break; + case "max": + res = Math.max(num1, num2); + break; + case "min": + res = Math.min(num1, num2); + break; + } + } + return res; + } +} diff --git a/Week8/src/Exercise2.java b/Week8/src/Exercise2.java new file mode 100644 index 0000000..a421222 --- /dev/null +++ b/Week8/src/Exercise2.java @@ -0,0 +1,61 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/20 - 8:28 + */ +public class Exercise2 { + public static void main(String[] args) { + String htmlStr = "欢迎访问武汉纺织大学主页\n" + + "\n" + + "首页\n" + + "教务处\n" + + "数计学院\n" + + "\n" + + "\n" + + ""; + + System.out.println("网页标题:" + getContext(htmlStr, "title").get(0)); + + List img = getAttribute(htmlStr, "img", "src"); + System.out.print("网页中共" + img.size() + "个图片,文件名为"); + for (int i = 0; i < img.size(); i++) { + System.out.print(img.get(i)); + if(i != img.size()-1) + System.out.print(","); + } + System.out.println(); + + List linkContext = getContext(htmlStr, "a"); + List linkHref = getAttribute(htmlStr, "a", "href"); + System.out.println("网页中包含" + linkContext.size() + "个超链接,超链接信息如下:\n名称\t地址"); + for (int i = 0; i < linkContext.size(); i++) { + System.out.println(linkContext.get(i) + "\t" + linkHref.get(i)); + } + } + + public static List getContext(String htmlStr, String tagName){ + List list = new ArrayList<>(); + String regex = "<" + tagName + "(.*?)>(.*?)"; + Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(htmlStr); + while (matcher.find()){ + list.add(matcher.group(2)); + } + return list; + } + + public static List getAttribute(String htmlStr,String tagName,String attributeName){ + List list = new ArrayList<>(); + String regex = "<" + tagName + ".+?" + attributeName + "='(.*?)'.+?>"; + Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(htmlStr); + while (matcher.find()){ + list.add(matcher.group(1)); + } + return list; + } +} diff --git a/Week8/src/Exercise3.java b/Week8/src/Exercise3.java new file mode 100644 index 0000000..cee8e2e --- /dev/null +++ b/Week8/src/Exercise3.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/20 - 8:47 + */ +public class Exercise3 { + public static void main(String[] args) { + String htmlStr = ""; + Pattern p1 = Pattern.compile("<\\s*font\\s*([^>]*)\\s*>",Pattern.CASE_INSENSITIVE); + Matcher m1 = p1.matcher(htmlStr); + if(m1.find()) { + String fontStr = m1.group(); + Pattern p2 = Pattern.compile("([a-z]+)\\s*=\\s*\"([^\"]+)\"",Pattern.CASE_INSENSITIVE); + Matcher m2 = p2.matcher(fontStr); + while (m2.find()) { + String eachAttribute = m2.group(); + eachAttribute = eachAttribute.replaceAll("=",":"); + eachAttribute = eachAttribute.replaceAll("\"",""); + System.out.println(eachAttribute); + } + } + } +} diff --git a/Week8/src/Test1.java b/Week8/src/Test1.java new file mode 100644 index 0000000..1aae872 --- /dev/null +++ b/Week8/src/Test1.java @@ -0,0 +1,32 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/19 - 9:09 + */ +public class Test1 { + public static void main(String[] args) { + String htmlStr = "...
xxxxxxxxx
"; + Pattern p = Pattern.compile("", Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(htmlStr); + int cnt=0; + while (matcher.find()){ + String imgStr = matcher.group().toLowerCase(); + System.out.println(imgStr); + String src = getSrc(imgStr); + System.out.println(src); + cnt++; + } + } + + public static String getSrc(String imgStr){ + Pattern p = Pattern.compile("(src)=(\"|\')(.*?)(\"|\')", Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(imgStr); + if(matcher.find()){ + return matcher.group(); + } + return ""; + } +} diff --git a/Week8/src/Test2.java b/Week8/src/Test2.java new file mode 100644 index 0000000..455b0da --- /dev/null +++ b/Week8/src/Test2.java @@ -0,0 +1,53 @@ +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/19 - 10:01 + */ +public class Test2 { + public static void main(String[] args) { + String regex = "((add)|(sub)|(max)|(min)|(doubleMe))\\(\\d+(.\\d+)?,\\d+(.\\d+)?\\)"; + Pattern p = Pattern.compile(regex); + String str = new Scanner(System.in).nextLine(); + Matcher matcher = p.matcher(str); + String res = ""; + while (matcher.find()){ + String subStr = matcher.group(); + res = calcExpression(subStr)+""; + System.out.print(subStr + " " + res + "->"); + str = str.replaceAll(regex,res); + System.out.println(str); + matcher = p.matcher(str); + } + System.out.println("运算结果:" + res); + } + + public static double calcExpression(String expression){ + double res = 0; + String exp = expression.substring(0, expression.indexOf("(")); + if ("doubleMe".equals(exp)){ + double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")"))); + res = num*2; + } else { + double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(","))); + double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")"))); + switch (exp){ + case "add": + res = num1+num2; + break; + case "sub": + res = num1-num2; + break; + case "max": + res = Math.max(num1, num2); + break; + case "min": + res = Math.min(num1, num2); + break; + } + } + return res; + } +} diff --git a/Week8/src/Test3.java b/Week8/src/Test3.java new file mode 100644 index 0000000..009c9f4 --- /dev/null +++ b/Week8/src/Test3.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/19 - 10:40 + */ +public class Test3 { + public static void main(String[] args) { + String htmlStr = "欢迎访问武汉纺织大学主页\n" + + "\n" + + "首页\n" + + "教务处\n" + + "数计学院\n" + + "\n" + + "\n" + + ""; + System.out.println(getContext(htmlStr, "a")); + System.out.println(getAttribute(htmlStr,"a")); + } + + public static List getContext(String htmlStr, String tagName){ + List list = new ArrayList<>(); + String regex = "<" + tagName + "(.*?)>(.*?)"; + System.out.println(regex); + Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(htmlStr); + while (matcher.find()){ + list.add(matcher.group(1)); + } + return list; + } + + public static List getAttribute(String htmlStr,String tagName){ + List list = new ArrayList<>(); + String regex = "<" + tagName + "(.*?)>(.*?)"; + Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(htmlStr); + while (matcher.find()){ + list.add(matcher.group(1).split("=")[1]); + } + return list; + } +}