正則表達式語法講解(三)

   

Modifiers
修改符
 
Modifiers are for changing behaviour of TRegExpr.
修改符用於改變TregExpr的行爲。
 
There are many ways to set up modifiers.
Any of these modifiers may be embedded within the regular expression itself using the (?...) construct.
Also, You can assign to appropriate TRegExpr properties (ModifierX for example to change /x, or ModifierStr to change all modifiers together). The default values for new instances of TRegExpr object defined in global variables, for example global variable RegExprModifierX defines value of new TRegExpr instance ModifierX property.
{TODO    下面的翻譯不好,懇請指教}
有很多方法設置修改符。
任意這些修改符都可以使用(?...)構造器被嵌入一個正則表達式。
同樣,你可以賦予TregExpr屬性合適的修改符(例如修改符X用於改變/x,或者修改符Srt用於改變所有的修改符)。TRegExpr對象的默認值定義在全局變量中,比如全局變量RegExprModifierX定義了TRegExpr的新實例的ModifierX屬性的值。
 
Do case-insensitive pattern matching (using installed in you system locale settings), see also InvertCase.
要進行大小寫敏感模式匹配(使用安裝在你係統的本地配置),參見InvertCase。
 
m
 
Treat string as multiple lines. That is, change "^'' and "$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string, see also Line separators.
處理多行文本。意味着,"^'' and "$''不僅匹配本行的開頭和結尾,而是目標串中的任意開頭和結尾,參見行分隔符。
 
s
 
Treat string as single line. That is, change ".'' to match any character whatsoever, even a line separators (see also Line separators), which it normally would not match.
處理目標串爲單行。表示改變".''用於匹配任意字符,即使是行分隔符(參加行分隔符),正常情況下行分隔符不會被".''匹配。
 
g
 
Non standard modifier. Switching it Off You'll switch all following operators into non-greedy mode (by default this modifier is On). So, if modifier /g is Off then '+' works as '+?', '*' as '*?' and so on
非標準修改符。關閉修改符,你將使下列的操作符爲非貪婪模式(默認修改符使開啓的)。所以,如果修改符/g關閉了,那麼'+'就會等於'+?','*'等於'*?',等等。
 
x
 
Extend your pattern's legibility by permitting whitespace and comments (see explanation below).
通過使用空格和註釋擴展你的匹配模式的易讀性。(參見下面的explanation)
 
r
 
Non-standard modifier. If is set then range ? additional include russian letter '?, ?? additional include '?, and ??include all russian symbols.
Sorry for foreign users, but it's set by default. If you want switch if off by default - set false to global variable RegExprModifierR.
非標準修改符。如果設置非標準修改符,那麼?的範圍就還會包括俄國的字符'?,??還包括'?和??等所有俄文符號。
對外國人來說很抱歉,這被設置爲默認的。如果你默認要求關閉 - 設置全局變量RegExprModifierR爲false即可。
 
The modifier /x itself needs a little more explanation. It tells the TRegExpr to ignore whitespace that is neither backslashed nor within a character class. You can use this to break up your regular expression into (slightly) more readable parts. The # character is also treated as a metacharacter introducing a comment, for example:
修改符/x需要一些更多的解釋。它告訴TRegExpr忽略既不是反斜線也不是在一個字符類中的空格。你可以使用這個特性輕鬆地使你的正則表達式分爲更多的可讀部分。“#”也被認爲使一個元字符用於註釋中,例如:
 
(
(abc) # comment 1
 |   # You can use spaces to format r.e. - TRegExpr ignores it
(efg) # comment 2
)
 
This also means that if you want real whitespace or # characters in the pattern (outside a character class, where they are unaffected by /x), that you'll either have to escape them or encode them using octal or hex escapes. Taken together{TODO 不曉得怎麼翻譯}, these features go a long way towards making regular expressions text more readable.
這也意味着如果你要在模式中使用真正的空格或者“#”(在一個字符類外,他們可以通過加/x被忽略),你要麼不得不將它們轉義,或者用十進制或者十六進制表示。同時,這些特性對正在表達式文本的可讀性大有幫助。
 
Perl extensions
Perl擴展
 
{TODO    不知道}
(?imsxr-imsxr)
You may use it into r.e. for modifying modifiers by the fly. If this construction inlined into subexpression, then it effects only into this subexpression
 
Examples:
 (?i)Saint-Petersburg       matchs 'Saint-petersburg' and 'Saint-Petersburg'
 (?i)Saint-(?-i)Petersburg matchs 'Saint-Petersburg' but not 'Saint-petersburg'
 (?i)(Saint-)?Petersburg    matchs 'Saint-petersburg' and 'saint-petersburg'
 
 ((?i)Saint-)?Petersburg    matchs 'saint-Petersburg', but not 'saint-petersburg'
 
 
(?#text)
A comment, the text is ignored. Note that TRegExpr closes the comment as soon as it sees a ")", so there is no way to put a literal ")" in the comment.
一個註釋,文本被忽略了。注意TRegExpr只要看到")"就關閉註釋,所以不能在註釋中加入")"。
 
Internal mechanism explanation
 
It seems You need some internal secrets of TRegExpr?
Well, it's under constraction, please wait some time..
Just now don't forget to read the FAQ (expecially 'non-greediness' optimization question).
如果需要更多幫助,暫時無法藉助internet,還是多看FAQ吧。8)
發佈了20 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章