1. Regular expressions
1 What is a regular expression
A regular expression is a formula used to verify that a string meets certain rules.
2 Create regular expressions
(1) Object form
var reg = new RegExp("Regular expressions");
Copy the code
If there is a “/” in the regular expression, use this one.
(2) Direct quantity form (generally used in this way)
var reg = / Regular expression /
Copy the code
Note: The regular expression itself is not quoted.
3. Use and Usage of regular expressions (3)
Model validation
Check whether a string, such as a cell phone number, an ID number, and so on, conforms to the norm.
Methods: An object that requires a regular expression to call a method:
Regular expression object. test(string variable) returns true or false
Example: Verify that the string contains the letter ‘o’
// Create a simple regular expression object
var reg = /o/;
// Create a string object as the target string
var str = 'Hello World! ';
// Call the test() method of the regular expression object to verify that the target string meets the pattern we specified, and return true
console.log("Does the string contain 'o'="+reg.test(str));
Copy the code
Match the read
Read the part of the target string that satisfies the rule, such as the mailbox address in the whole text.
Methods:
The string variable. Match (the regular expression object) returns an array.
Example: Read all the ‘L’ letters in a string
// Match read: reads all 'L' letters in a string
// If g is not used, only the first match will be found
//1. Write a regular expression
var reg2 = /l/g
//2. Use the regular expression to read the string
var arr = str.match(reg2);
console.log(arr)
Copy the code
“/l/” is the regular expression, and g specifies the entire string. If not specified, the default is to find the first one that matches the regular expression, and the method will return directly.
Match to replace
Replace the parts of the target string that meet the criteria with other strings, such as replacing “Hello” with “hi” in the entire text.
Methods:
String object. replace(regular expression, new character to replace)
Note that this replacement returns a new string, not a modification of the original string, and requires an object to receive the return value.
Example: Replace the first ‘o’ in a string with ‘@’
var reg = /o/;
var str = 'Hello World! ';
var newStr = str.replace(reg,The '@');
// Only the first o is replaced, indicating that our regular expression can only match the first string that satisfies
console.log("str.replace(reg)="+newStr);//Hell@ World!
// The original string is not changed, but a new string is returned
console.log("str="+str);//str=Hello World!
Copy the code
If you want to replace all of them, add g to the end of the regular expression, where g stands for full-text lookup.
4 Matching mode of the regular expression
The full text search
If the regular expression object is not decorated with G, only the first match is returned when the regular expression is used for a lookup; When g is used, all matches are returned.
/[A-Z]/ capital letters.
Example: Find the number of uppercase letters. (Match read)
// The target string
var targetStr = 'Hello World! ';
// Use a globally matched regular expression
var reg = /[A-Z]/g;
// Get all matches
var resultArr = targetStr.match(reg);
// The array length is 2
console.log("resultArr.length="+resultArr.length);
// Iterate through the array and find "H" and "W"
for(var i = 0; i < resultArr.length; i++){
console.log("resultArr["+i+"] ="+resultArr[i]);
}
Copy the code
Ignore case
Add I to the end of the regular expression
Example: Get the number of all ‘o’ in a string. (Match read)
// The target string
var targetStr = 'Hello WORLD! ';
// Use a case-insensitive regular expression
var reg = /o/gi;
// Get all matches
var resultArr = targetStr.match(reg);
// The array length is 2
console.log("resultArr.length="+resultArr.length);
// Iterate through the array to get 'o' and 'o'
for(var i = 0; i < resultArr.length; i++){
console.log("resultArr["+i+"] ="+resultArr[i]);
}
Copy the code
Full-text search and case-ignore can be mixed, and g and I are sequential
Multi-row lookup
When a multi-line lookup is not used, the target character is parsed as a single line with or without a newline.
/Hello$/ indicates the end of Hello, and m indicates the multi-line lookup mode.
Example: Determine if there is a string ending in ‘Hello’. (Schema validation)
// The target string is 1
var targetStr01 = 'Hello\nWorld! ';
// The target string 2
var targetStr02 = 'Hello';
// Matches a regular expression ending in 'Hello', using multiple lines of matching
var reg = /Hello$/m;
console.log(reg.test(targetStr01));//true
console.log(reg.test(targetStr02));//true
Copy the code
Multi – line lookup and ignore case can also be mixed, m and I do not distinguish before and after.
5 yuan character
Characters that are assigned special meanings in regular expressions cannot be used as ordinary characters. To match the metacharacter itself, you need to escape the metacharacter by prefixing it with a \, such as: ^
Common metacharacters
code | instructions |
---|---|
. | Matches any character except the newline character. |
\w | Matching letters or numbers or underscores is equivalent to [A-Za-Z0-9_] |
\W | Matches any non-word character. Equivalent to [^ A Za – z0-9 _] |
\s | Matches any whitespace, including Spaces, tabs, feed characters, and so on. Is equivalent to [\f\n\r\t\v]. |
\S | Matches any non-whitespace character. This is equivalent to [^\f\n\r\t\v]. |
\d | Match the number. This is equivalent to [0-9]. |
\D | Matches a non-numeric character. Equivalent to [^ 0-9] |
\b | Matches the beginning or end of a word |
^ | Matches the beginning of the string, but uses the inverse in [] |
$ | Matches the end of the string |
6 character set
Syntax format | The sample | instructions |
---|---|---|
[Character list] | Regular expression: [ABC] Meaning: The target string contains any character in ABC Target string: plain Match or not: Yes Cause: the “a” in plain is in the list “ABC” | Any character in the target string that appears in the character list is considered a match. |
[^ character list] | [^ ABC] Meaning: Target string contains any character other than ABC Target string: plain Match: Yes Cause: plain contains “p”, “l”, “I”, “n” | Matches any character not included in the character list. |
[Character range] | Regular expression: [A-z] Meaning: lists all lowercase characters. Regular expression: [A-z] Meaning: lists all uppercase characters | Matches any character in the specified range. |
Number of occurrences of 7 characters
code | instructions |
---|---|
* | Zero or more times |
+ | Occur once or more times |
? | Zero or one occurrence |
{n} | A n time |
{n,} | N or more times |
{n,m} | N to m times |
8 “or” in regular Expressions
Use the symbol “|”
Example:
// The target string
var str01 = 'Hello World! ';
var str02 = 'I love Java';
// Match 'World' or 'Java'
var reg = /World|Java/g;
console.log("str01.match(reg)[0]="+str01.match(reg)[0]);//World
console.log("str02.match(reg)[0]="+str02.match(reg)[0]);//Java
Copy the code
— — — — — — — — — — — — — — — — — — –
Some commonly used character expressions
demand | Regular expressions |
---|---|
The user name | / ^ [a zA – Z_] [a – zA – Z_ – 0-9] {5, 9} $/ |
password | / ^ [a zA – Z0 – _ – @ # & * 9] {6, 12} $/ |
A space before and after | /^\s+|\s+$/g |
/^[a-zA-Z0-9_.-]+@([a-zA-Z0-9-]+[.]{1})+[a-zA-Z]+$/ |
2. XML files
XML is called Extensible Markup Language.
1. XML file and HTML file
The same
Both are markup languages and are made up of markup tags.
2 Tags The syntax of the tags is similar
The difference between
1. Different tags: XML is a custom tag, HTML is a predefined tag
2 different functions: XML is used to store data, is an interactive data format [configuration file], HTML is used to display data and data collection.
2 USES XML
1 storage data, interactive data
XML, myBatis. XML, spring. XML, springmVC.xml, etc.). It is important to note that when XML is used in a configuration file, the constraint [DTD, XSD] is added to the XML file.
3 Parse XML files using Java
Analytic mode classification
1 DOM parsing: the document is loaded into memory, forming a DOM tree (document object), encapsulating the various parts of the document into some objects.
Advantages: Since the DOM tree is formed in memory, programmers can manipulate the XML file in an object-oriented manner. Writing code is very convenient, and the DOM tree can be added, deleted, modified and looked up.
Cons: DOM trees are memory intensive and slow to parse. Therefore, DOM parsing is usually not used when parsing large XML files
SAX parsing: read line by line, event-driven, parse line by line, release line by line, very small memory footprint
Common parser
When parsing XML using Java code, we usually don’t parse XML directly using the NATIVE DOM or SAX built-in in the JDK because the code is too complex. Some companies and organizations already have good XML parsers wrapped around them, and we often use third-party XML parsers to parse XML.
1 JAXP: Parsing provided by Sun. Dom and SAX support. (Not often used)
2 JDOM
3 DOM4J (common)
use
First we need to import the dependencies before we can use the SAXReader parser.
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
Copy the code
A SAXReader parser, which uses its read method to read in a file, returns a value called a Document object.
Step (get all the elements in the XML) :
- Creating a parser
- Through the parser SAXReader, the XML is parsed into a Document object
- Through the document, get the root element in the XML
- Get all child elements from the root element
- Traverse the collection
- To get the data
try {
//1. Create a parser
SAXReader saxReader = new SAXReader();
//2. Parse the XML into a Document object by using the SAXReader parser
Document document = saxReader.read("day04_xml/students.xml");
//3. Obtain the root element in XML by document
Element rootElement = document.getRootElement();
//4. Obtain all child elements from the root element
List<Element> elements = rootElement.elements();
//5. Iterate over the set
for (Element element : elements) {
//6. Obtain data
String id = element.attributeValue("id");
String name = element.elementText("name");
String age = element.elementText("age");
System.out.println("id = " + id);
System.out.println("name = " + name);
System.out.println("age = "+ age); }}catch (DocumentException e) {
e.printStackTrace();
}
Copy the code
Iii. Tomcat server
1 Server classification
Hardware server
Advanced computers: Computers that provide services for other customers to access computers
Software server
Software: The server installed on the computer can provide services to other computers in the network, mapping local files into a virtual URL for others to access the network.
Common software servers are as follows:
- Tomcat (Apache) : the most widely used JavaWeb server
- JBoss (Redhat) : support JavaEE, application is relatively wide EJB container ->SSH lightweight framework instead.
- GlassFish (Orcale) : Oracle develops a JavaWeb server, which is not widely used
- Resin (Caucho) : support JavaEE, more and more widely used
- Weblogic (Orcale) : Pay. JavaEE support, suitable for large projects
- Websphere (IBM) : Paid. JavaEE support, suitable for large projects
2 Tomcat profile
Tomcat download address
tomcat.apache.org/
Tomcat configuration
- Prepare the JAVA_HOME environment
- To decompress the Tomcat directory, set path to the Tomcat directory on the classpath.
Tomcat directory structure
- Bin: stores executable script files, such as startup.bat and shutdown.bat
- Conf: stores configuration files, such as servlet. XML, web. XML, and logging.properties
- Lib: stores jar packages, such as jsp-api.jar and servlet-api.jar
- Logs: Stores log files
- Temp: Stores temporary files
- Webapps: Stores the default tomcat project
- Work: Tomcat workspace, session serialization file, Java file after JSP parsing, etc
Using Tomcat
- Start the server (open startup.bat in bin)
- Access server [Default server port: 8080]
- http://localhost:8080
- http://127.0.0.1:8080
- http://ip address: 8080
Example Change the Tomcat port number
-
Port number range: 1-65535, [1-1024 is unavailable]
-
The recommended value is 8080-8089
-
tomcat->conf->server.xml
<Connector port="8088" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
Copy the code
Find this line in the server.xml file and change the port value.
The startup server has garbled characters
Modify the conf->logging.properties file
Find the line shown below
If you open startup.bat again, there will be no garbled characters.
3 Integrate Tomcat into Idea
404 Symptom: The requested resource is not found
Start the server. By default, if you access 8080, you will access the index page under the ROOT project
Default Server configuration
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> Copy the code
Principle: Integrating Tomcat into IDEA is to integrate The Tomcat image into IDEA. The image file depends on the local Tomcat
Image file reference path: C:\Users\ 86132.intellijidea2019.2 \system\tomcat
steps
Step 1.
Step 2.
Step 3.
Step 4.
4 Create a dynamic project and deploy Tomcat of IDEA
Dynamic engineering project directory structure
Directory or file name | function |
---|---|
SRC directory | Store Java source files |
A web directory | Hosts Web development related resources |
Web/web – INF directory | XML file, classes directory, and lib directory |
Web/web – INF/web. XML file | Alias: Deployment descriptor; Function: A core configuration file for a Web project |
Web/web – INF/classes directory | Store compiled *.class bytecode files |
Web/web – INF/lib directory | Store third-party JAR packages |
Have a JavaEE view
Step 1.
Step 2.
Step 3.
No JavaEE view
Step 1.
Step 2.
Start Tomcat to run dynamic project
Step 1.
Step 2.
Step 3.
Step 4.