Greedy mode and lazy mode in regular expressions

rule:

  • Greedy pattern is to match as much as possible
  • Lazy mode is as few matches as possible

 

Such as the string <div> a123 </ div> <div> b321 </ div>

  • Using the regular expression <div>. * </ Div> matches the entire string <div> a123 </ div> <div> b321 </ div>
  • If you want to match only the content of the first div tag <div> a123 </ div>, use the regular expression <div>. *? </ Div>

 

to sum up:

  • Regular expression, which means that the string repeats a few characters? + * {} Defaults to greedy mode
  • To switch to lazy mode, add one after the metacharacter? 

 

 


[Java interview questions and answers] sorting recommendations

 

Published 562 original articles · praised 1543 · 1.65 million views +

Guess you like

Origin blog.csdn.net/meism5/article/details/105547870