📄 00000002.htm
字号:
1 <BR>% set match <BR>http <BR>% regexp {^[^:]+(?=.*\.edu$)} $x match <BR>0 <BR>% regexp {^[^:]*(?!.*\.edu$)} $x match <BR>1 <BR>% set match <BR>http <BR> <BR>The regular expressions above may seem complicated, but they're really <BR>not bad! Find the lookahead expression in the first regexp command <BR>above; it starts with (?= and ends at the corresponding parenthesis. The <BR> "guts" of this lookahead expression is .*\.com$, which stands for "a <BR>string that ends with .com". So the first regexp command above matches <BR>any string containing non-colon (:) characters, as long as the rest of <BR>the string ends with .com. The second regexp is similar but looks for <BR>a string ending with .edu. Because regexp returns 0, you can see that <BR>this doesn't match. The third regexp looks for a string not ending <BR>with .edu. It matches because $x ends with .com. <BR>Tcl 8.1 lets you document complex regular expressions by embedding <BR>comments. See the next section. <BR> <BR>Switches <BR>Tcl 8.1 added command switches to regexp and regsub. For a complete <BR>list, see the commands' reference pages. Let's look at two of the most <BR>important changes. <BR>Complex REs can be difficult to document. The -expanded switch sets <BR>expanded syntax, which lets you add comments within a regular <BR>expression. Comments start with a # character; whitespace is ignored. <BR>This is mostly for scripting -- but you can also use it on a command <BR>line, as we'll do in the example below. Let's look the same RE twice: <BR>first in the standard compact syntax, and second in expanded syntax: <BR> <BR> <BR>% set x <A HREF="http://www.ajubasolutions.com">http://www.ajubasolutions.com</A> <BR><A HREF="http://www.ajubasolutions.com">http://www.ajubasolutions.com</A> <BR>% regexp {^[^:]+(?=.*\.com$)} $x match <BR>1 <BR>% set match <BR>http <BR>% regexp -expanded { <BR> ^ # beginning of string <BR> [^:]+ # all characters to the first colon <BR> (?= # begin positive lookahead <BR> .*\.com$ # for a trailing .com <BR> ) # end positive lookahead <BR>} $x match <BR>1 <BR>% set match <BR>http <BR> <BR>In expanded syntax, you can use space and tab characters to indent and <BR>make your code clear. To enter actual space and tab characters into your <BR> RE, use the escapes \s and \t, respectively. <BR>The other important new switch we'll cover here is -line. It enables <BR>newline-sensitive matching. By default (without -line), Tcl regular <BR>expressions have always treated newlines as an ordinary character. For <BR>example, if a string contains several lines (separated by newline <BR>characters), the end-of-string anchor $ wouldn't match at any of the <BR>embedded newlines. To write code that matched line-by-line, you had to <BR>read input lines one by one and do separate matches against each line. <BR> <BR>With the -line switch, the metacharacters ^, $, ., and [] treat a <BR>newline as the end of a "line." So, for example, the regular <BR>expression ^San Jose matches the second line of input below: <BR> <BR> <BR>% set x {Dolores Sanchez <BR>San Jose, CA} <BR>Dolores Sanchez <BR>San Jose, CA <BR>% regexp {^San Jose} $x match <BR>0 <BR>% regexp -line {^San Jose} $x match <BR>1 <BR>% set match <BR>San Jose <BR> <BR>The -line switch actually enables two other switches. You can set part <BR>of the features from -line by choosing one of these switches instead: <BR>The -lineanchor switch makes ^ and $ match at the beginning and end of a <BR> line. The -linestop switch makes . and [] stop matching at a newline <BR>character. <BR>Options, Directors <BR>This section introduces two more features from Tcl 8.1. Details are in <BR>the re_syntax(n) reference page. <BR>An 8.1 RE can start with embedded options. These look like (?xyz), where <BR> xyz are one or more option letters. For instance, (?i)ouch matches OUCH <BR> because i is the "case-insensitive matching" option. Other options <BR>include (?e), which marks the rest of the regular expression as an 8. <BR>0-style RE -- to let you avoid confusion with the new 8.1 syntax. <BR> <BR>An RE can also start with three asterisks, which is a director. For <BR>example, ***= is the director that says the rest of the regular <BR>expression is literal text. So the RE ***=(?i)ouch matches exactly <BR>(?i)ouch; the (?i) isn't treated as an option. <BR> <BR>Part 3. Summary: Regular Expression Changes in Tcl 8.1 <BR>Tcl 8.1 added advanced regular expression syntax. The new re_syntax(n) <BR>reference page has details. <BR>This table below summarizes the new syntax: {m} Matches m instances of <BR>the previous pattern item <BR>{m}? Matches m instances of the previous pattern item. Non-greedy. <BR>{m,} Matches m or more instances of the previous pattern item. <BR>{m,}? Matches m or more instances of the previous pattern item. <BR>Non-greedy. <BR>{m,n} Matches m through n instances of the previous pattern item. <BR>{m,n}? Matches m through n instances of the previous pattern item. <BR>Non-greedy. <BR>*? Matches zero or more of the previous pattern item. Non-greedy. <BR>+? Matches one or more of the previous pattern item. Non-greedy. <BR>?? Matches zero or one of the previous pattern item. Non-greedy. <BR>(?:re) Groups a subpattern, re, but does not capture the result. <BR>(?=re) Positive lookahead. Matches the point where re begins. <BR>(?!re) Negative lookahead. Matches any point where re does not begin. <BR>\c One of many backslash escapes. <BR>[. .] Delimits a collating element within a bracketed expression. <BR>[= =] Delimits an equivalence class within a bracketed expression. <BR>[: :] Delimits a character class within a bracketed expression. <BR>(?abc) Embedded options a, b, and c <BR>*** Director <BR> <BR> <BR>Some of the new switches for regexp and regsub are: -expanded Enable <BR>expanded syntax (for comments) <BR>-line Enable newline-sensitive matching <BR>-linestop Make [] and . stop at newlines. <BR>-lineanchor Make ^ and $ match the start and end of a line. <BR> <BR> <BR> <BR> <BR> <BR>-- <BR> 桃花坞里桃花庵,桃花庵下桃花仙;桃花仙人种桃树,又摘桃花卖酒钱。 <BR> 酒醒只在花前坐,酒醉换来花下眠;半醒半醉日复日,花落花开年复年。 <BR> 但愿老死花酒间,不愿鞠躬车马前;车尘马足富者趣,酒盏花枝贫者缘。 <BR> 若将富贵比贫贱,一在平地一在天;若将贫贱比车马,他得驱驰我得闲。 <BR> 别人笑我忒疯癫,我笑他人看不穿;不见五陵豪杰墓,无花无酒锄做田。 <BR> <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.204.7.234] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -