Jdk1.4-formal expression

Source: Internet
Author: User
What is a regular expression? (reqular expressions)

It is a special representation of the response case and string for search and replace.

Because many system settings on UNIX are stored in the text box, the network or program design often needs to be searched and replaced.

Therefore, a special command is displayed, called a formal expression.

We can use the "S/</g" Regular Expression to convert all characters containing "<" into "lt ;"

Therefore, jdk1.4 provides a set of standard-expression packages for your use.

If jdk1.4 or lower, you can go to http://jakarta.apache.org/oroto get the package with the same function.

A string of characters "s/</g" listed by the regular expression is the positive expression.

Principal used in j2sdk1.4

"." Represents any character

Regular Expression string of the original string
. AB
. ABC AB

"+" Represents one or more characters
"*" Indicates zero or more characters.

Regular Expression string of the original string
+ AB
* ABC

"()" Group

Regular Expression string of the original string
(AB) * aabab Abab

Type

Regular Expression string of the original string
[A-dA-D0-9] * abcza0 abca0
[^ A-d] * abe0 E0
[A-d] * abcdefgh Abab

Simplified

/D equals to [0-9]
/D is equal to [^ 0-9] non-numeric characters
/S equals to [/T/N/x0b/F/R] white space characters
/S is equal to [^/T/N/x0b/F/R] non-blank characters
/W for [a-zA-Z_0-9] data or English text
/W and so on [^ a-zA-Z_0-9] non-digital and English text

Start or end of each line

^ Indicates the start of each line
$ Indicates the end of each line

--------------------------------------------------------------------------------

Class differences between regular expression and Java. util. RegEx

Pattern-difference of positive expression
Matcher-the result of past Normalization
Patternsyntaxexpression-exception thrown while attempting to compile a regular expression

Example 1: replace all characters in the string that match "<" with "lt ;"

Import java. Io .*;
Import java. util. RegEx .*;
/**
* Replace all characters in the string that match "<" with "lt ;"
*/
Public static void replace01 (){
// Bufferedreader lets us read line-by-line
Reader r = new inputstreamreader (system. In );
Bufferedreader BR = new bufferedreader (R );
Pattern pattern = pattern. Compile ("<"); // search for all characters that match the '<' character
Try {
While (true ){
String line = Br. Readline ();
// Null Line means input is exhausted
If (line = NULL)
Break;
Matcher A = pattern. matcher (line );
While (A. Find ()){
System. Out. println ("the character found is" + A. Group ());
}
System. Out. println (A. replaceall ("lt;"); // replace all matching characters with <;
}
} Catch (exception ex) {ex. printstacktrace ();};
}

Example 2:

Import java. Io .*;
Import java. util. RegEx .*;
/**
* Similar to the stringtokenizer Function
* Separate the string with "," and the token is the longest.
*/
Public static void search01 (){
// Bufferedreader lets us read line-by-line
Reader r = new inputstreamreader (system. In );
Bufferedreader BR = new bufferedreader (R );
Pattern pattern = pattern. Compile (", // s *"); // search for all "," characters in a string
Try {
While (true ){
String line = Br. Readline ();
String words [] = pattern. Split (line );
// Null Line means input is exhausted
If (line = NULL)
Break;
//-1 means we haven't found a word yet
Int longest =-1;
Int longestlength = 0;
For (INT I = 0; I <words. length; ++ I ){
System. Out. println ("segmentation:" + words [I]);
If (words [I]. Length ()> longestlength ){
Longest = I;
Longestlength = words [I]. Length ();
}
}
System. Out. println ("maximum length:" + words [longest]);
}
} Catch (exception ex) {ex. printstacktrace ();};
}

--------------------------------------------------------------------------------

Other legal provisions

/^/S * # ignore blank characters starting from each line
(M (S | r | RS)/.) # meets the requirements of ms., mrs., And mr. (titles)

--------------------------------------------------------------------------------

Additional information

Http://java.sun.com/

Http://www.javalobby.org/

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.