Regular Expression-Java programming (BASICS) Learning Diary

Source: Internet
Author: User

During the Chinese New Year's Eve, the indoor temperature was high (I wore short sleeves and even had to wear an shirt in my own house). I felt that my learning efficiency was a little low. Today I saw the regular expression.

Example 4:

Assume that the name contains most of the 25 letters, and the first letter is capitalized. The pattern of surname can be described

[A-Z] [A-Za-Z] []

It seems strange. First, the usage of [a-Za-Z] is not seen in the list of common Regular Expressions (but it can be used );

What's more, it should be a typographical error. How is it 25 letters? In addition, the [] at the end should be one of the 11-lane 24 at the end. The positive solution seems to be {}, which is a length limit after manual testing.

It's hard to understand. I don't know whether the book is wrong or not.

 

Another sentence is that the identifier mode can be described as [A-Za-Z _ $] [// W $]. There are not many articles in the book, and it seems like a parallel relationship, for example, [A-Za-Z _ $] is a letter or _ or $.

In addition, Java identifiers are defined in section 2.3: they cannot start with a number. They should only be rules. Writing [1-9] [A-Z] [/d] can also be written, so the identifier mode is described...

 

I did not take a closer look at the front. I found that I had a small rule, so I had to remember more. // D number, // D is not a number ..... // One character of w words and one character of W non-words .. // S is a white space character, // s is a non-white space character, and the case is complementary

 

 

 

 

 

 

Exercise review after class:

A few cannot understand, mainly those * +? Those

Package review_08;

Public class review_8_22 {
Public static void main (string [] ARGs ){
System. Out. println ("Java". replaceall ("[AV]", "RX"); // both A and V match a and V.
System. Out. println ("Java". replaceall ("Av", "RX "));
System. Out. println ("Java". replaceall ("[a] [v]", "RX"); // you cannot understand
System. Out. println ("Java". replacefirst ("// w *", "RX"); // 1st incomprehensible

System. Out. println ("Java". replaceall ("// w *", "RX"); // 2nd incomprehensible

System. Out. println ("Java". replaceall ("// W +", "RX"); // 3rd incomprehensible
System. Out. println ("Java". replaceall ("// W +? "," RX "); // 4th incomprehensible

System. Out. println ("Java". replacefirst ("// W +? "," RX "); // 5th incomprehensible


}
}

Stick point data

Greed and laziness

When a regular expression contains a qualifier that can accept duplicates, the common behavior is (when the entire expression can be matched) matching.As many as possible. Take this expression as an example: A. * B, which will match the longest string starting with a and ending with B. If you use it to searchAababThen, it will match the entire string aabab. This is calledGreedyMatch.

Sometimes, we need moreLazinessMatch, that is, matchAs few as possible. All the qualifiers given above can be converted to the lazy match mode, as long as a question mark is added after it ?. This way .*? This means to match any number of duplicates, but use the minimum number of duplicates if the entire match is successful. Now let's look at the lazy version example:

A .*? B matches the string that is shortest, starts with a, and ends with B. If you apply itAababIt will match AAB (first to third character) and AB (fourth to fifth character ).

Table 5. Lazy delimiters
Code/syntax Description
*? Repeat any time, but as few as possible
+? Repeat once or more times, but as few as possible
?? Repeated 0 or 1 times, but as few as possible
{N, m }? Repeat n to m times, but as few as possible
{N ,}? Repeated more than N times, but as few as possible

 

 

Why is the first match AAB (the first to the third character) rather than AB (the second to the third character )? Simply put, because a regular expression has another rule, it has a higher priority than a lazy/greedy rule: the first match to start has the highest priority-the match that begins earliest wins.

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.