In Java Regular Expressions (.*?) Vs (.*)

Source: Internet
Author: User

@ Author YHC


[Java]
Public static void main (String [] args ){
 
// Select the minimum and maximum number of repetitions
String a = "{user = \" administrator \ ", password = \" root \"}";
String B = "user = \" a \ ""; // note that there is a space between =
String c = "user \ r \ n = \" c \ ""; // note that there is a line break

// Obtain the content between double quotation marks and obtain the names of all users in a, B, and c respectively.
String regex = "(? S) user .*? = (\".*? \")";
Match (regex, a); // user = "administrator"
Match (regex, B); // user = ""
Match (regex, c); // user
// = "C"

// The above is very simple. Pay attention to the following examples:
// Select the minimum and maximum number of duplicates
// Note the following: user = may contain spaces.
// The result we need is to find the user = 'xxx' information.
String notice = "[{user = 'admin', password = 'admin'}," +
"{User = 'yhc ', password = 'yhc'}]";
// You may think .*? Why? So let's take a look.
// Yes? And none? Difference
String regex1 = "user. * = '(.*?) '"; // Between user. * =?
Match (regex1, notice );
// Output result (error): user = 'admin', password = 'admin'}, {user = 'yhc ', password = 'yhc'

String regex2 = "user .*? = '(.*?) '"; // Between user. * =?
Match (regex2, notice );
// Output result (correct ):
// User = 'admin'
// User = 'yhc'

// Why is the above one? Is there such a big difference?
// Explain the above Code Running Mechanism
// With the first user in the matched text, the engine will arrive at (. *), where the dot (.) can
// Match any character, including a line break. If the asterisk (*) is used, it is repeated 0 or more times. Note that,
// The asterisk (*) is greedy, so the Star (. *) matches all
// Content, note ======>>>> (. *) eats all the content of the target file, from the first match
// Start from the user's location;

// When (. *) is full, the engine will try to match = at the end of the target text
// This expression, of course, will definitely fail to match, but it does not mean that this match is over,
// The regular expression engine performs backtrack. After a match is made
//, The regular expression will save a backtracking position. If a regular expression matches a text that fails
// Then the Regular Expression Engine can return to these locations
// When the = match fails, the engine goes back to let (. *) discard one of its matches.
// Character followed by = will be matched again, the position of the last character in the text,
// If it still fails, the engine will repeat the last 2nd characters in the file to try matching.
// This process continues until the = match is successful. If the match is not successful, the final result (.*)
// All backend locations are used up, and the regular expression matching fails.

// If the matching is successful at a certain point in the whole backtracking process, the matching will be continued.
// Failed, the engine goes back, this process will be repeated until = '(.*?) 'Until it can be matched

// The problem occurs on. *, because the asterisk is greedy, so pay attention to it!
// Do we need to use lazy to put one behind it? Make it lazy
// Example :(*?), (+ ?), (??) ,{ 1,100 }? Can be changed to laziness.

}

Public static void match (String regex, String input ){
// Create a regular object
Pattern pattern = Pattern. compile (regex );
// Matched object
Matcher matcher = pattern. matcher (input );
// Output matching string
While (matcher. find ()){
System. out. println (matcher. group ());
}
}

Public static void main (String [] args ){

// Select the minimum and maximum number of repetitions
String a = "{user = \" administrator \ ", password = \" root \"}";
String B = "user = \" a \ ""; // note that there is a space between =
String c = "user \ r \ n = \" c \ ""; // note that there is a line break

// Obtain the content between double quotation marks and obtain the names of all users in a, B, and c respectively.
String regex = "(? S) user .*? = (\".*? \")";
Match (regex, a); // user = "administrator"
Match (regex, B); // user = ""
Match (regex, c); // user
// = "C"

// The above is very simple. Pay attention to the following examples:
// Select the minimum and maximum number of duplicates
// Note the following: user = may contain spaces.
// The result we need is to find the user = 'xxx' information.
String notice = "[{user = 'admin', password = 'admin'}," +
"{User = 'yhc ', password = 'yhc'}]";
// You may think .*? Why? So let's take a look.
// Yes? And none? Difference
String regex1 = "user. * = '(.*?) '"; // Between user. * =?
Match (regex1, notice );
// Output result (error): user = 'admin', password = 'admin'}, {user = 'yhc ', password = 'yhc'

String regex2 = "user .*? = '(.*?) '"; // Between user. * =?
Match (regex2, notice );
// Output result (correct ):
// User = 'admin'
// User = 'yhc'

// Why is the above one? Is there such a big difference?
// Explain the above Code Running Mechanism
// With the first user in the matched text, the engine will arrive at (. *), where the dot (.) can
// Match any character, including a line break. If the asterisk (*) is used, it is repeated 0 or more times. Note that,
// The asterisk (*) is greedy, so the Star (. *) matches all
// Content, note ======>>>> (. *) eats all the content of the target file, from the first match
// Start from the user's location;

// When (. *) is full, the engine will try to match = at the end of the target text
// This expression, of course, will definitely fail to match, but it does not mean that this match is over,
// The regular expression engine performs backtrack. After a match is made
//, The regular expression will save a backtracking position. If a regular expression matches a text that fails
// Then the Regular Expression Engine can return to these locations
// When the = match fails, the engine goes back to let (. *) discard one of its matches.
// Character followed by = will be matched again, the position of the last character in the text,
// If it still fails, the engine will repeat the last 2nd characters in the file to try matching.
// This process continues until the = match is successful. If the match is not successful, the final result (.*)
// All backend locations are used up, and the regular expression matching fails. www.2cto.com

// If the matching is successful at a certain point in the whole backtracking process, the matching will be continued.
// Failed, the engine goes back, this process will be repeated until = '(.*?) 'Until it can be matched

// The problem occurs on. *, because the asterisk is greedy, so pay attention to it!
// Do we need to use lazy to put one behind it? Make it lazy
// Example :(*?), (+ ?), (??) ,{ 1,100 }? Can be changed to laziness.

}
 
Public static void match (String regex, String input ){
// Create a regular object
Pattern pattern = Pattern. compile (regex );
// Matched object
Matcher matcher = pattern. matcher (input );
// Output matching string
While (matcher. find ()){
System. out. println (matcher. group ());
}
}


Author: yhc13429826359

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.