Java Regular Expressions

Source: Internet
Author: User

(1) pattern Pt = pattern. Compile ("</S * body [^>] *> .*? </S */body/S *> ", pattern. case_insensitive | pattern. dotall );

Matcher Mt = pt. matcher (htmlcontent );

While (mt. Find ()){
String content = Mt. Group ();
Content = content. replaceall ("</S * body [^>] *> ",""). replaceall ("</S */body/S *> ",""). replaceall ("/R |/N ","");
If (content. matches ("// s * (</S * P [^>] *>) * (& nbsp;) * // s * (null) * // s * (& nbsp;) * (</S */P>) * // s *"))
Return true;
}

 

 

Use the following statement:

(2) pattern Pt = pattern. Compile ("</S * body [^>] *> (. |/n |/R )*? </S */body/S *> ", pattern. case_insensitive );

Matcher Mt = pt. matcher (htmlcontent );

While (mt. Find ()){
String content = Mt. Group ();
Content = content. replaceall ("</S * body [^>] *> ",""). replaceall ("</S */body/S *> ",""). replaceall ("/R |/N ","");
If (content. matches ("// s * (</S * P [^>] *>) * (& nbsp;) * // s * (null) * // s * (& nbsp;) * (</S */P>) * // s *"))
Return true;
}

But the content is large enough, because Mt. Group (); uses recursive queries to conform to regular characters, but (. |/n |/R )*? By searching through three branches, when the content is large enough

Memory overflow occurs when the memory size is insufficient. Therefore, regular matching with the expression (1) can improve the performance without causing memory overflow.

 

 

Pattern. case_insensitive -- enable case-insensitive matching. (? I)

Pattern. Comments -- allow blank spaces and comments in the mode. (? X)

Pattern. dotall -- enable the dotall mode. In dotall mode, expression. Matches any character, including the line terminator. By default, this expression does not match the line terminator. (? S)

Pattern. Literal -- enable mode literal parsing. When this flag is specified, the input string in the specified mode is treated as a string of literal characters. Metacharacter in the input sequence or transfer
The sequence has no special meaning. When used with this flag, the flag case_insensitive and unicode_case reserve their impact on matching.

Pattern. multiline -- enable multiline mode. In multi-row mode, the expressions ^ and $ are just before or after the row terminator or the end of the input sequence. By default, these expressions only match the beginning and end of the entire input sequence. You can also embed a flag expression (? M) Start the multiline mode.

 

Embedded flag expression

Regular
Quantity

Equivalent embedding sign expression

Pattern. canon_eq

None

Pattern. case_insensitive

(? I)

Pattern. Comments

(? X)

Pattern. multiline

(? M)

Pattern. dotall

(? S)

Pattern. Literal

None

Pattern. unicode_case

(? U)

Pattern. unix_lines

(? D)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.