`
java-mans
  • 浏览: 11453989 次
文章分类
社区版块
存档分类
最新评论

正则习点 --- 10

 
阅读更多

Chapter 4. The Mechanics ofExpression Processing

4.1. Start Your Engines!

4.1.1. Regex Engine Types

ØDFA (Deterministic FiniteAutomaton)

ØNFA (Nondeterministic FiniteAutomaton)

使用NFA的工具包括.NET、PHP、Ruby、Perl、Python、GNU Emacs、ed、sec、vi、grep的多数版本,甚至还有某些版本的egrep和awk。

4.1.2. Testing the Engine Type

如果我们想知道工具a或工具b使用何种引擎,写一段代码即可!

#! /usr/bin/perl -w

$testStr = "nfa not";

$testStr =~ /(nfa)|(nfa not)/;

print $testStr;

此代码测试Perl使用何种引擎。

4.2. Match Basics

4.2.1 About the Example

两条普适(DFA和NFA)的原则:

²优先选择最左端(最靠开头)的匹配结果。

²标准的匹配量词(「*」, 「+」, 「?」, and 「{m,n}」)是匹配优先的。

4.2.2. Rule 1: The Match ThatBegins Earliest Wins

匹配先从需要查找的字符串的起始位置尝试匹配。(the match is first attempted at the very beginning of the string tobe search (just before the first character))

所以,用「cat」匹配下列字符串:

Thedragging belly indicates that your cat is too fat.

结果是indicates,而不是后来出现的cat。单词cat是能够被匹配的,但是indicates中的cat出现的更早,所以得到匹配的是他。

4.2.3. Rule 2: The Standard Quantifiers Are Greedy

标准匹配量词总是尝试匹配尽可能多的字符,直到匹配上限(maximum allowed)为止。

4.3. Regex-Directed VersusText-Directed

4.3.1. NFA Engine:Regex-Directed

表达式中的控制权在不同的元素之间转换,我们称之为“表达式主导”。

引擎会不断地尝试匹配表达式中的元素,直到匹配成功或是报告失败。

4.3.2. DFA Engine: Text-Directed

扫描的字符串中的每个字符都对引擎进行了控制,我们称这种方式为“文本主导”。

DFA引擎在扫描字符串时,会纪录“当前有效(currently in the works)”的所有匹配可能。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics