Home >Backend Development >PHP Tutorial >Boolean Education Regular Expression Video Tutorial
By using regular expressions to represent text search parameters, some dynamically changing string matching, such as changing time, can be cleverly completed. Therefore, we have collected the "Boolean Education Regular Expression Video Tutorial", hoping to help everyone better understand regular expressions.
## Course playback address: http://www.php .cn/course/281.html
The teacher’s teaching style:
The more difficult point in this video is greedy and non-greedy:
#1. What are greedy and non-greedy in regular expressions? Greedy matching For example:String str="abcaxc"; Patter p="ab*c";Greedy matching: Regular expressions generally tend to match to the maximum length, which is the so-called greedy matching. For example, if the pattern p is used to match the string str, the result is: abcaxc(ab*c). Non-greedy matching: Just match the result with as few matching characters as possible. For example, if the pattern p is used to match the string str, the result is: abc(ab*c). 2. How to distinguish between the two modes in programming The default is greedy mode; add a question mark directly after the quantifier? It is non-greedy mode. Quantifiers: {m,n}: m to n *: any number of +: one to multiple ? :0 or a 3. Program exampleUse Snort rules to use a part of a rule as matching text to match the content part.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegularTest { public static void main(String[] arg){ String text="(content:\"rcpt to root\";pcre:\"word\";)"; String rule1="content:\".+\""; //贪婪模式 String rule2="content:\".+?\""; //非贪婪模式 System.out.println("文本:"+text); System.out.println("贪婪模式:"+rule1); Pattern p1 =Pattern.compile(rule1); Matcher m1 = p1.matcher(text); while(m1.find()){ System.out.println("匹配结果:"+m1.group(0)); } System.out.println("非贪婪模式:"+rule2); Pattern p2 =Pattern.compile(rule2); Matcher m2 = p2.matcher(text); while(m2.find()){ System.out.println("匹配结果:"+m2.group(0)); } } }
Here we also recommend downloading source code resources: //m.sbmmt.com/xiazai /code/2132
The above is the detailed content of Boolean Education Regular Expression Video Tutorial. For more information, please follow other related articles on the PHP Chinese website!