Java processes (p) tags in html code to ensure closure
Requirements:
Processing a piece of html code
Tag, so that it is not nested, closed, and no content is exposed outside the tag
For example:
11111
2222233333
44444
55555566666
Mind Map:
Code:
Public static String checkpp (String content) {String Str; int time = 0; int pos = 0; int path = 0; boolean flag = true; int index1 = 0; int index2 = 0; // ensure that the start and end positions are respectivelyAnd
Int firstindex = content. indexOf (""); If (firstindex! = 0) {content ="
"+ Content;} int lastindex = content. lastIndexOf ("
"); If (lastindex! = Content. length ()-4 | lastindex =-1) {content = content +"";}Stringbuffer sb = new StringBuffer (content); do {time ++; System. out. println ("-------" + time + "----- path:" + path); switch (path) {case 0: index1 = sb. indexOf ("", Pos + 1); index2 = sb. indexOf ("
", Pos + 1); System. out. println (sb. toString (); System. out. println ("pos:" + pos); System. out. println ("index1:" + index1); System. out. println ("index2:" + index2); if (index1! =-1) {if (index2
Path = 1; pos = index2;} else {//
(...)*
→
(Xxx )*
Sb = sb. insert (index1 ,"
"); Path = 0; pos = index1 + 4;} else {// no more
But it's not end yet:
(...
)*
Path = 1; pos = index2;} break; case 1: index1 = sb. indexOf ("
", Pos + 1); index2 = sb. indexOf ("
", Pos + 1); System. out. println (sb. toString (); System. out. println ("pos:" + pos); System. out. println ("index1:" + index1); System. out. println ("index2:" + index2); // no more
Exitif (index2 =-1) {flag = false; break;} // no more
But not endif (index1 =-1) {sb = sb. insert (pos + 4 ,"
"); Path = 0; pos = pos + 7; break;} else {/*
(....)*
* Optional * pos */if (index1
4 ){//
(Xxx)
→
(Xxx)
Sb = sb. insert (pos + 4 ,"
"); Sb = sb. insert (index1 + 3 ,"
"); Path = 0; pos = index1 + 7;} else if (index1-pos = 4 ){//
Path = 0; pos = index1;} else {//
(....)*
→
(...)*
Sb = sb. insert (pos + 4 ,"
"); Path = 1; pos = index2 + 3;} break;} default: break;} System. out. println ("pos:" + pos); System. out. println (sb. toString (); // exitif (pos = sb. length ()-4) break;} while (flag); Str = sb. toString (); Str = Str. replaceAll ("
(\ S )*()*()*
"," "); Return Str ;}
Log of the execution example:
111112222233333
44444
55555566666-------1----- path:011111
2222233333
44444
55555566666pos:0index1:8index2:11pos:1211111
2222233333
44444
55555566666-------2----- path:011111
2222233333
44444
55555566666pos:12index1:24index2:15pos:1511111
2222233333
44444
55555566666-------3----- path:111111
2222233333
44444
55555566666pos:15index1:24index2:32pos:3111111
22222
33333
44444
55555566666-------4----- path:011111
22222
33333
44444
55555566666pos:31index1:43index2:39pos:3911111
22222
33333
44444
55555566666-------5----- path:111111
22222
33333
44444
55555566666pos:39index1:43index2:51pos:4311111
22222
33333
44444
55555566666-------6----- path:011111
22222
33333
44444
55555566666pos:43index1:61index2:51pos:5111111
22222
33333
44444
55555566666-------7----- path:111111
22222
33333
44444
55555566666pos:51index1:61index2:64pos:6811111
22222
33333
44444
555555
66666-------8----- path:011111
22222
33333
44444
555555
66666pos:68index1:-1index2:71pos:7111111
22222
33333
44444
555555
66666-------9----- path:111111
22222
33333
44444
555555
66666pos:71index1:-1index2:80pos:7811111
22222
33333
44444
555555
66666
-------10----- path:011111
22222
33333
44444
555555
66666
pos:78index1:-1index2:83pos:8311111
22222
33333
44444
555555
66666
11111
22222
33333
44444
555555
66666
Analysis:
1. Because it only involves
, You can
And
It is processed as two States. As shown in the Mind Map, the string is processed from left to right.
2. Convert String to StringBuffer to facilitate insertion.
3. Use the outer do while switch layer to traverse.
4. Use pos to record the current target location.
5. If flag is used, set it to false when the termination condition is triggered in swtich, and then break to terminate.
6. trigger termination signal: a. The current pos is located in the last one
This condition applies to both States. B. The current pos node does not traverse backward
Appears to prove that this is the last
, That is, the end.
Note: Because
The status is not the end state, so you cannot exit directly from case 0 of the switch. You can only jump to case 1.