Java basics: Regular Expressions and reflection
1. Regular Expression 1. Overview 1. concepts:
Regular Expressions, also known as regular and regular expressions, are a concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.
Many programming languages Support string operations using regular expressions.
A regular expression is a logical formula for string operations. It uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings.
2. Functions
Given a regular expression and another string, we can achieve the following goals:
1. Whether the given string conforms to the filtering logic of the regular expression (called "match ");
2. You can use a regular expression to obtain the desired part from the string.
3. Regular Expressions are characterized:
1. Strong flexibility, logic, and functionality;
2. You can quickly implement complex String Control in an extremely simple way.
3. It is difficult for new contacts.
2. Common Function 1: Matching
Matches method of String
/*** QQ number verification requirement: a number with a length of 5-15. It cannot start with 0 */private static void fun2 () {String s = 023; boolean B = s. matches ([1-9] [0-9] * {4, 14}); B = s. matches ([1-9] \ d * {4, 14}); // equal to System. out. println (B );}
2. Cutting
Split () method of String
/*** Cut Method */private static void fun3 () {// cut space String s = afdf dfsfd dafa; String [] names = s. split (+); for (String name: names) {System. out. println (name);} // cut the stacked word String s1 = afdfaaaaadfsfdssdafa; String [] myNames = s. split ((.) \ 1 +); for (String name: myNames) {System. out. println (name );}}
3. Replace
String replaceAll () method
/*** Replace */private static void fun4 () {String s1 = afdfaaaaadfsfdssdafa; String s = s1.replaceAll ((.) \ 1 +, $1); System. out. println (s );}
4. Obtain
Matcher's find () method to find the next subsequence
The group () method of Matcher, which returns the input subsequence matched by the previous matching operation.
/*** Get */private static void fun5 () {Pattern p = Pattern. compile (a * B); // encapsulate a regular rule into an object Matcher m = p. matcher (aaaaabaaabdfdfaaabss); // The matched string is encapsulated into the object boolean B = m. matches (); // m. find (); // System. out. println (m); while (m. find () {System. out. println (m. group ());}}
3. Web Crawler
Web Crawlers sound good, but the truth is very simple, that is, getting data from specified rules on the Internet
The specified rule can be written using a regular expression.
public class PaChong {public static void main(String[] args) throws Exception {List
list = fun1();for (String name: list) {System.out.println(name);}}/** * @throws Exception * */private static List
fun1() throws Exception {URL url = new URL(http://127.0.0.1:8080/myweb/test.html);InputStream in = url.openStream();BufferedReader bufr = new BufferedReader(new InputStreamReader(in));ArrayList
list = new ArrayList
();String main_rgex = \w+@\w+(\.\w+)+;Pattern p =Pattern.compile(main_rgex);String line = null;while((line = bufr.readLine()) != null){Matcher m = p.matcher(line);while(m.find()){list.add(m.group());}}return list;}}
Ii. Reflection
I. Overview
1. Definition
The JAVA reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known;
Any method and attribute of an object can be called;
This kind of dynamically obtained information and the function of dynamically calling object methods is called the reflection mechanism of java language.
Dynamically obtains information in a class, that is, java reflection.
2. Reflection benefits
Reflection technology greatly improves program scalability
3. the Java reflection mechanism provides the following functions:
Determine the class to which any object belongs at runtime;
Construct any class object at runtime;
Judge the member variables and methods of any class at runtime;
Call methods of any object at runtime;
Generate dynamic proxy
Ii. Obtain the Class Object
public static void getClassObject_3() throws ClassNotFoundException {String className = cn.itcast.bean.Person;Class clazz = Class.forName(className);System.out.println(clazz);}
3. Obtain the constructor in the Class.
/*** Create an instance with parameters * @ throws Exception */private static void fun2 () throws Exception {String name = jinfulin. b. bean; Class clazz = Class. forName (name); Constructor cons = clazz. getConstructor (String. class, int. class); Object obj = cons. newInstance (sf, 21);}/*** create a new instance without parameters */private static void fun1 () throws ClassNotFoundException, InstantiationException, IllegalAccessException {String name = jinfulin. p. bean. person; Class clazz = Class. forName (name); Object obj = clazz. newInstance (); // create a new instance without parameters. out. println (obj );}
4. Obtain fields in the Class
Field field = null; clazz. getField (age); // only public field = clazz. getDeclaredField (age) can be obtained; // only this class is obtained, but private is included. // Cancel the permission check for access to private fields. Brute-force access. Field. setAccessible (true); private static void getFileDemo () throws Exception {Class clazz = Class. forName (jinfulin. p. bean. person); Field field = null; field = clazz. getDeclaredField (name); // only obtains the private fields in this class. Field. setAccessible (true); // cancel permission check on private fields. Brute-force access. Object obj = clazz. newInstance (); field. set (obj, Jin Fulin); Object o = field. get (obj); System. out. println (o );}
4. Obtain methods in Class
Method [] methods = clazz. getMethods (); // obtain all public methods. Methods = clazz. getDeclaredMethods (); // only obtain all methods in this class, including private. Method. invoke (obj, Xiaoqiang, 89); // set the parameter value to public static void getMethodDemo_3 () throws Exception {Class clazz = Class. forName (cn. itcast. bean. person); Object obj = clazz. newInstance (); Method method = clazz. getMethod (paramMethod, String. class, int. class); method. invoke (obj, Xiaoqiang, 89 );}
Iii. Final
Regular Expressions are simple to use, but they are complex and have poor readability. However, some common string regular expressions such as email addresses and ID card numbers can be found online.
Reflection technology is very important. reflection can also be used in combination with the abstract factory model to replace the database. The reflection knowledge will be discussed later.