Home  >  Article  >  Backend Development  >  Boolean Education Regular Expression Video Tutorial

Boolean Education Regular Expression Video Tutorial

黄舟
黄舟Original
2017-08-30 09:36:021559browse

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.


Boolean Education Regular Expression Video Tutorial


## Course playback address: http://www.php .cn/course/281.html


The teacher’s teaching style:

Teacher The lectures are simple and in-depth, clear in structure, analyzed layer by layer, interlocking, rigorous in argumentation, and rigorous in structure. The logical power of thinking is used to attract students' attention, and rationality is used to control the classroom teaching process. By listening to teachers' lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by teachers' rigorous academic attitude.


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 example

Use 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: http://www.php.cn/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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn