首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >该模式的适当正则表达式是什么?

该模式的适当正则表达式是什么?
EN

Stack Overflow用户
提问于 2015-04-30 03:09:29
回答 1查看 83关注 0票数 0

我有一个文本文件,其中包含所有CS课程的课程目录。实际的课程本身很容易找到,因为它是在线的开头,从课程开始。但前提条件对我来说有点棘手。我可以找到有先决条件的行,但是prereq课程可以是一个或多个,用逗号和" and“分隔。有时,在prereq语句后面还有一行,其中包含其他课程名称,但不包含prereq本身。下面是prereq文件的示例:

代码语言:javascript
复制
CS 4213. Computing for Bioinformatics. (3-0) 3 Credit Hours.
Prerequisite: CS 1173 or another programming course. Emphasizes computing tasks common in bioinformatics: variables, flow control, input/output, strings, pattern matching, arrays, hash tables, functions, access to databases, and parsing data from queries for common bioinformatics tasks. SQL, XML, and BioPerl. May not be applied to the 24 hours of required electives for computer science majors, but may be included for a computer science minor.
CS 4313. Automata, Computability, and Formal Languages. (3-0) 3 Credit Hours.
Prerequisites: CS 3341 and CS 3343. Discussion of abstract machines (finite state automata, pushdown automata, and Turing machines), formal grammars (regular, context-free, and type 0), and the relationship among them.
CS 4353. Unix and Network Security. (3-0) 3 Credit Hours.
Prerequisite: CS 3433. A technical survey of the fundamentals of computer and information security. Issues include cryptography, authentication, attack techniques at both the OS and network level, defense techniques, intrusion detection, scan techniques and detection, forensics, denial of service techniques and defenses, libpcap, libdnet and libnet programming.
CS 4363. Cryptography. (3-0) 3 Credit Hours.
Prerequisites: CS 3341, CS 3343, and CS 3433. A course in pure and applied cryptography, with emphasis on theory. Topics may include conventional and public-key cryptosystems, signatures, pseudo-random sequences, hash functions, key management, and threshold schemes.
CS 4383. Computer Graphics. (3-0) 3 Credit Hours.
Prerequisites: CS 2121, CS 2123, CS 3341, and CS 3343. An introduction to two- and three-dimensional generative computer graphics. Display devices, data structures, mathematical transformations, and algorithms used in picture generation, manipulation, and display.
CS 4393. User Interfaces. (3-0) 3 Credit Hours.
Prerequisite: CS 3443. Study of advanced user interface issues. User interface design, human factors, usability, GUI programming models, and the psychological aspects of human-computer interaction.
CS 4413. Web Technologies. (3-0) 3 Credit Hours.
Prerequisites: CS 3421 and CS 3423. Fundamentals of Web and component technology: markup languages, layout design, client and server side programming, database and Web integration.
CS 4593. Topics in Computer Science. (3-0) 3 Credit Hours.
Prerequisite: Consent of instructor. Advanced topics in an area of computer science. May be repeated for credit when topics vary.
CS 4633. Simulation. (3-0) 3 Credit Hours.
Prerequisites: CS 3341 and CS 3343. Design, execution, and analysis of simulation models, discrete event simulation techniques, input and output analysis, random numbers, and simulation tools and languages.
CS 4713. Compiler Construction. (3-0) 3 Credit Hours.
Prerequisites: CS 3341, CS 3343, CS 3841, and CS 3843. An introduction to implementation of translators. Topics include formal grammars, scanners, parsing techniques, syntax-directed translation, symbol table management, code generation, and code optimization. (Formerly titled “Compiler Writing.”).

这就是我现在拥有的:

代码语言:javascript
复制
Pattern p = Pattern.compile("^(CS [0-9][0-9][0-9][0-9]).*");
Pattern p2 = Pattern.compile("^Prereq.* ([A-Z]* [0-9][0-9][0-9][0-9]).*");
while ((line = br.readLine()) != null) {
    Matcher m = p.matcher(line);
    if (m.find()) {
        System.out.println(m.group(1));
    }
    Matcher m2 = p2.matcher(line); 
    if (m2.find()) {
        System.out.println("Prereq: "+m2.group(1)+", Occurrences: "+m2.groupCount());
        //System.out.println(line);
    }
}

到目前为止,这得到了所有的课程和第一个先决条件,或者没有,如果没有预先的课程。

样本输出:

代码语言:javascript
复制
CS 4213
Prereq: CS 1173, Occurrences: 1
CS 4313
Prereq: CS 3343, Occurrences: 1
CS 4353
Prereq: CS 3433, Occurrences: 1
CS 4363
Prereq: CS 3433, Occurrences: 1
CS 4383
Prereq: CS 3343, Occurrences: 1
CS 4393
Prereq: CS 3443, Occurrences: 1
CS 4413
Prereq: CS 3423, Occurrences: 1
CS 4593
CS 4633
Prereq: CS 3343, Occurrences: 1
CS 4713
Prereq: CS 3843, Occurrences: 1

所以说,4313,我想要CS 3341和CS 3343

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-30 03:58:18

使用以下三种模式应该更容易:

代码语言:javascript
复制
    Pattern p = Pattern.compile("^(CS [0-9][0-9][0-9][0-9]).*");
    Pattern p2 = Pattern.compile("^Prereq");
    Pattern p3 = Pattern.compile("[A-Z]+ [0-9]{4}");
    while ((line = br.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (m.find()) {
            System.out.println(m.group(1));
        }
        Matcher m2 = p2.matcher(line);
        if (m2.find()){
            final Matcher m3 = p3.matcher(line);
            while (m3.find()) {
                System.out.println("Prereq: " + m3.group(0));
            }
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29958343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档