血常规一般查什么病| 这个季节吃什么水果最好| 热疹用什么药| mrd是什么意思| 公举是什么意思啊| 阴阳两虚吃什么药| 相对是什么意思| 小水滴会变成什么| 跟腱是什么| 医政科是做什么的| 什么冰淇淋最贵| 孕妇为什么不能参加婚礼| 来月经喝啤酒有什么影响| 六一送女孩子什么礼物| 护肝片什么时候吃最好| 肝实质回声欠均匀是什么意思| 女性吃辅酶q10有什么好处| 钠低是什么原因| 炎细胞浸润是什么意思| 有什么四字词语| 耽美剧是什么意思| 春天穿什么衣服| 什么然起什么| 糟卤是什么| 肚子胀气吃什么| 开荤什么意思| 结婚下雨有什么说法| pr值是什么意思| 为什么高铁没有e座| 仓鼠怀孕有什么特征| 梦见玫瑰花是什么预兆| 南京有什么好吃的| girls是什么意思| 糖尿病人可以吃什么零食| psp是什么意思| 结婚10年是什么婚| 螺蛳粉为什么叫螺蛳粉| 如火如荼什么意思| 宁字属于五行属什么| 一什么雨伞| 宫颈糜烂是什么原因引起的| 提前吃什么药喝酒不醉| 肩周炎是什么症状| 黄酮是什么| 手舞足蹈是什么生肖| 什么样的莲蓬| 七夕节吃什么| 胎儿停止发育是什么原因造成的| 金鱼的尾巴像什么| 屌丝是什么| 高粱是什么| 为什么头顶会痛| 四叶草代表什么| 什么是处男| 网贷是什么| 6月18日什么星座| 周瑜属什么生肖| baumwolle是什么面料| 女人喜欢什么样的男人| 汉尼拔是什么意思| leep术是什么手术| 什么泡酒让性功能最强| 美商是什么意思| showroom是什么意思| 魔芋是什么东西| 青光眼什么症状| 熊猫喜欢吃什么食物| 闰月是什么意思| 威图手机为什么那么贵| 69岁属什么| 贫血喝什么茶| 膝盖窝疼是什么原因| 包馄饨用猪肉什么部位| 梦见性生活是什么意思| 严重失眠吃什么药最好| 走之旁与什么有关| 生吃大葱有什么好处和坏处| 什么病不能吃空心菜| 吃生红枣有什么好处| 人心果什么时候成熟| 折耳根什么味道| 属马的女生和什么属相最配| 吃什么能让头发变黑| 古尔邦节是什么意思| 柠檬泡蜂蜜有什么功效| 氯雷他定为什么比西替利嗪贵| 什么的城堡| 卵巢囊性包块是什么意思| 9月25号什么星座| sm是什么| 鱼泡是鱼的什么器官| 属猪的贵人属相是什么| 腰间盘突出睡觉什么姿势好| 尿ph值高是什么意思| 肝郁脾虚吃什么药效果最好| 高血压为什么不能献血| 青稞是什么东西| 早上10点是什么时辰| 牙签肉是什么肉| 黑管是什么乐器| 什么炖鸡汤好喝又营养| 碳酸钠呈什么性| hrd是什么| 血液循环不好吃什么药| 开车穿什么鞋最好| jsdun是什么牌子的手表| 什么的糯米| 脑血栓是什么意思| 血压高看什么科| 海瓜子是什么| k9什么意思| 九肚鱼是什么鱼| 什么是纳米| 忻字五行属什么| 华西医院院长什么级别| 胃溃疡吃什么药好得快| 男性粘液丝高什么原因| 刮宫和清宫有什么区别| 肛门痒痒的是什么原因| 湿气重吃什么能去湿气| 什么地响| 兔死狐悲指什么生肖| 什么产品美白效果最好最快| 缺血灶是什么意思| 看空是什么意思| 杂面是什么面| 刺身是什么意思| 魔芋丝是什么做的| 兔子五行属什么| 高温天气喝什么茶解暑| 三尖瓣反流什么意思| 艾灸肚脐眼有什么好处| 土豆是什么科| 凝视的近义词是什么| 肥猪拱门是什么生肖| 经常放臭屁是什么原因| 为什么会一直咳嗽| 小孩子头晕是什么原因| 石膏是什么| 足度念什么| 测五行缺什么| 龋齿是什么原因造成的| 指甲月牙代表什么意思| 羊水少吃什么| lining是什么意思| 小孩脱发是什么原因引起的| 冬日暖阳是什么意思| 夭折是什么意思| 阴道口瘙痒用什么药| 策字五行属什么| 孕期不能吃什么| 甲不开仓财物耗散是什么意思| 舌强语謇是什么意思| 脸上长痤疮用什么药| 男生生日送什么礼物好| 口腔溃疡吃什么药好的快| 月经量少吃什么药调理| 阻断是什么意思| 六堡茶是什么茶| 蚊子代表什么生肖| 智齿什么时候拔最好| 肠胃炎能吃什么| 磨玻璃影是什么意思| 七月三号是什么日子| 三七甘一是什么意思| 回忆杀是什么意思| carol什么意思| 梦见去墓地是什么预兆| 雪纺是什么面料| 较真的人是什么性格| 送父亲什么礼物好| 04年出生属什么| 真谛是什么意思| 人工荨麻疹是什么原因引起的| lf是什么牌子| 地黄长什么样| 输血前四项检查是什么| 分散片是什么意思| 脑梗前有什么预兆| it是什么意思| 三点水及念什么| 6月12日是什么日子| 什么药可以推迟月经| 一年四季穿棉衣是什么生肖| 慌张是什么意思| 房间放什么可以驱蜈蚣| 女人左下眼皮跳是什么预兆| 汗斑用什么药膏| 吃了兔子肉不能吃什么| 梦见背死人是什么意思| 提莫是什么意思| 两岁宝宝不会说话但什么都知道| 手掌心发红是什么原因| 避孕药是什么原理| 宫颈炎用什么药物治疗比较好| 滋生是什么意思| 尿道下裂是什么意思| 蜂蜜什么时候喝比较好| aj和nike什么关系| 嘴巴干苦是什么原因| 熬夜有什么坏处| 96615是什么电话| 潜血阳性是什么意思| 维生素c不能和什么一起吃| 胆结石属于什么科| 有什么鱼| 玄关是什么意思| 章鱼是什么动物| 屡禁不止的意思是什么| 口臭用什么牙膏| smeg什么品牌| 女人什么年龄性最旺| 意有所指是什么意思| 梦女是什么意思| 女性尿检能查出什么病| 什么的挑选| 鸡肉炒什么好吃| 嘴唇发紫什么原因| 石灰的主要成分是什么| 背锅侠是什么意思| 什么食物降血糖| cif是什么意思| 1月1号是什么星座| 什么叫弱视| 叶绿素是什么| m是什么意思| 万能受血者是什么血型| 鹿代表什么生肖| 为什么会有痰| 尿酸高不能吃什么食物| 青春痘用什么药膏擦最好呢| 心脏是什么组织| 什么一气| 穿什么衣服| 做b超可以检查出什么| 女性白带多吃什么药| 热泪盈眶的盈是什么意思| 琼瑶什么意思| 潘海利根香水什么档次| 榴莲不能跟什么一起吃| 常吃山药有什么好处和坏处| 旻什么意思| dk是什么牌子| 蜂蜜是什么糖| 随遇而安是什么生肖| 为什么门牙突然有缝了| 点完痣要注意什么| 药敏试验是什么意思| 排尿困难是什么原因| 为什么身上痒一抓就起疙瘩| 心慌气短胸闷吃什么药| 心电图pr间期缩短是什么意思| 前列腺炎用什么药| 纳财适合做什么| 双肺纤维灶是什么意思| 成也萧何败也萧何什么意思| 噤若寒蝉是什么生肖| 半夜两点是什么时辰| jojo是什么意思| 草朋刀是什么字| 车厘子与樱桃有什么区别| 脐炎用什么药| 梦见别人结婚是什么征兆| 狸猫换太子什么意思| 百度
The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
A newer edition of this document exists here

9. Regular Expressions

Regular Expressions (REs) provide a mechanism to select specific strings from a set of character strings.

Regular expressions are a context-independent syntax that can represent a wide variety of character sets and character set orderings, where these character sets are interpreted according to the current locale. While many regular expressions can be interpreted differently depending on the current locale, many features, such as character class expressions, provide for contextual invariance across locales.

The Basic Regular Expression (BRE) notation and construction rules in Basic Regular Expressions shall apply to most utilities supporting regular expressions. Some utilities, instead, support the Extended Regular Expressions (ERE) described in Extended Regular Expressions; any exceptions for both cases are noted in the descriptions of the specific utilities using regular expressions. Both BREs and EREs are supported by the Regular Expression Matching interface in the System Interfaces volume of IEEE Std 1003.1-2001 under regcomp(), regexec(), and related functions.

9.1 Regular Expression Definitions

For the purposes of this section, the following definitions shall apply:

entire regular expression

The concatenated set of one or more BREs or EREs that make up the pattern specified for string selection.

matched

A sequence of zero or more characters shall be said to be matched by a BRE or ERE when the characters in the sequence correspond to a sequence of characters defined by the pattern.

Matching shall be based on the bit pattern used for encoding the character, not on the graphic representation of the character. This means that if a character set contains two or more encodings for a graphic symbol, or if the strings searched contain text encoded in more than one codeset, no attempt is made to search for any other representation of the encoded symbol. If that is required, the user can specify equivalence classes containing all variations of the desired graphic symbol.

The search for a matching sequence starts at the beginning of a string and stops when the first sequence matching the expression is found, where "first" is defined to mean "begins earliest in the string". If the pattern permits a variable number of matching characters and thus there is more than one such sequence starting at that point, the longest such sequence is matched. For example, the BRE "bb*" matches the second to fourth characters of the string "abbbc", and the ERE "(wee|week)(knights|night)" matches all ten characters of the string "weeknights".

Consistent with the whole match being the longest of the leftmost matches, each subpattern, from left to right, shall match the longest possible string. For this purpose, a null string shall be considered to be longer than no match at all. For example, matching the BRE "\(.*\).*" against "abcdef", the subexpression "(\1)" is "abcdef", and matching the BRE "\(a*\)*" against "bc", the subexpression "(\1)" is the null string.

When a multi-character collating element in a bracket expression (see RE Bracket Expression) is involved, the longest sequence shall be measured in characters consumed from the string to be matched; that is, the collating element counts not as one element, but as the number of characters it matches.

BRE (ERE) matching a single character

A BRE or ERE that shall match either a single character or a single collating element.

Only a BRE or ERE of this type that includes a bracket expression (see RE Bracket Expression) can match a collating element.

BRE (ERE) matching multiple characters

A BRE or ERE that shall match a concatenation of single characters or collating elements.

Such a BRE or ERE is made up from a BRE (ERE) matching a single character and BRE (ERE) special characters.

invalid

This section uses the term "invalid" for certain constructs or conditions. Invalid REs shall cause the utility or function using the RE to generate an error condition. When invalid is not used, violations of the specified syntax or semantics for REs produce undefined results: this may entail an error, enabling an extended syntax for that RE, or using the construct in error as literal characters to be matched. For example, the BRE construct "\{1,2,3\}" does not comply with the grammar. A conforming application cannot rely on it producing an error nor matching the literal characters "\{1,2,3\}".

9.2 Regular Expression General Requirements

The requirements in this section shall apply to both basic and extended regular expressions.

The use of regular expressions is generally associated with text processing. REs (BREs and EREs) operate on text strings; that is, zero or more characters followed by an end-of-string delimiter (typically NUL). Some utilities employing regular expressions limit the processing to lines; that is, zero or more characters followed by a <newline>. In the regular expression processing described in IEEE Std 1003.1-2001, the <newline> is regarded as an ordinary character and both a period and a non-matching list can match one. The Shell and Utilities volume of IEEE Std 1003.1-2001 specifies within the individual descriptions of those standard utilities employing regular expressions whether they permit matching of <newline>s; if not stated otherwise, the use of literal <newline>s or any escape sequence equivalent produces undefined results. Those utilities (like grep) that do not allow <newline>s to match are responsible for eliminating any <newline> from strings before matching against the RE. The regcomp() function in the System Interfaces volume of IEEE Std 1003.1-2001, however, can provide support for such processing without violating the rules of this section.

The interfaces specified in IEEE Std 1003.1-2001 do not permit the inclusion of a NUL character in an RE or in the string to be matched. If during the operation of a standard utility a NUL is included in the text designated to be matched, that NUL may designate the end of the text string for the purposes of matching.

When a standard utility or function that uses regular expressions specifies that pattern matching shall be performed without regard to the case (uppercase or lowercase) of either data or patterns, then when each character in the string is matched against the pattern, not only the character, but also its case counterpart (if any), shall be matched. This definition of case-insensitive processing is intended to allow matching of multi-character collating elements as well as characters, as each character in the string is matched using both its cases. For example, in a locale where "Ch" is a multi-character collating element and where a matching list expression matches such elements, the RE "[[.Ch.]]" when matched against the string "char" is in reality matched against "ch", "Ch", "cH", and "CH".

The implementation shall support any regular expression that does not exceed 256 bytes in length.

9.3 Basic Regular Expressions

9.3.1 BREs Matching a Single Character or Collating Element

A BRE ordinary character, a special character preceded by a backslash, or a period shall match a single character. A bracket expression shall match a single character or a single collating element.

9.3.2 BRE Ordinary Characters

An ordinary character is a BRE that matches itself: any character in the supported character set, except for the BRE special characters listed in BRE Special Characters.

The interpretation of an ordinary character preceded by a backslash ( '\' ) is undefined, except for:

9.3.3 BRE Special Characters

A BRE special character has special properties in certain contexts. Outside those contexts, or when preceded by a backslash, such a character is a BRE that matches the special character itself. The BRE special characters and the contexts in which they have their special meaning are as follows:

.[\
The period, left-bracket, and backslash shall be special except when used in a bracket expression (see RE Bracket Expression). An expression containing a '[' that is not preceded by a backslash and is not part of a bracket expression produces undefined results.
*
The asterisk shall be special except when used:
^
The circumflex shall be special when used as:
$
The dollar sign shall be special when used as an anchor.

9.3.4 Periods in BREs

A period ( '.' ), when used outside a bracket expression, is a BRE that shall match any character in the supported character set except NUL.

9.3.5 RE Bracket Expression

A bracket expression (an expression enclosed in square brackets, "[]" ) is an RE that shall match a single collating element contained in the non-empty set of collating elements represented by the bracket expression.

The following rules and definitions apply to bracket expressions:

  1. A bracket expression is either a matching list expression or a non-matching list expression. It consists of one or more expressions: collating elements, collating symbols, equivalence classes, character classes, or range expressions. The right-bracket ( ']' ) shall lose its special meaning and represent itself in a bracket expression if it occurs first in the list (after an initial circumflex ( '^' ), if any). Otherwise, it shall terminate the bracket expression, unless it appears in a collating symbol (such as "[.].]" ) or is the ending right-bracket for a collating symbol, equivalence class, or character class. The special characters '.', '*', '[', and '\' (period, asterisk, left-bracket, and backslash, respectively) shall lose their special meaning within a bracket expression.

    The character sequences "[.", "[=", and "[:" (left-bracket followed by a period, equals-sign, or colon) shall be special inside a bracket expression and are used to delimit collating symbols, equivalence class expressions, and character class expressions. These symbols shall be followed by a valid expression and the matching terminating sequence ".]", "=]", or ":]", as described in the following items.

  2. A matching list expression specifies a list that shall match any single-character collating element in any of the expressions represented in the list. The first character in the list shall not be the circumflex; for example, "[abc]" is an RE that matches any of the characters 'a', 'b', or 'c'. It is unspecified whether a matching list expression matches a multi-character collating element that is matched by one of the expressions.

  3. A non-matching list expression begins with a circumflex ( '^' ), and specifies a list that shall match any single-character collating element except for the expressions represented in the list after the leading circumflex. For example, "[^abc]" is an RE that matches any character except the characters 'a', 'b', or 'c'. It is unspecified whether a non-matching list expression matches a multi-character collating element that is not matched by any of the expressions. The circumflex shall have this special meaning only when it occurs first in the list, immediately following the left-bracket.

  4. A collating symbol is a collating element enclosed within bracket-period ( "[." and ".]" ) delimiters. Collating elements are defined as described in Collation Order. Conforming applications shall represent multi-character collating elements as collating symbols when it is necessary to distinguish them from a list of the individual characters that make up the multi-character collating element. For example, if the string "ch" is a collating element defined using the line:

    collating-element <ch-digraph> from "<c><h>"
    
    

    in the locale definition, the expression "[[.ch.]]" shall be treated as an RE containing the collating symbol 'ch', while "[ch]" shall be treated as an RE matching 'c' or 'h'. Collating symbols are recognized only inside bracket expressions. If the string is not a collating element in the current locale, the expression is invalid.

  5. An equivalence class expression shall represent the set of collating elements belonging to an equivalence class, as described in Collation Order. Only primary equivalence classes shall be recognized. The class shall be expressed by enclosing any one of the collating elements in the equivalence class within bracket-equal ( "[=" and "=]" ) delimiters. For example, if 'a', 'à', and 'â' belong to the same equivalence class, then "[[=a=]b]", "[[=à=]b]", and "[[=â=]b]" are each equivalent to "[aàâb]". If the collating element does not belong to an equivalence class, the equivalence class expression shall be treated as a collating symbol.

  6. A character class expression shall represent the union of two sets:

    1. The set of single-character collating elements whose characters belong to the character class, as defined in the LC_CTYPE category in the current locale.

    2. An unspecified set of multi-character collating elements.

    All character classes specified in the current locale shall be recognized. A character class expression is expressed as a character class name enclosed within bracket-colon ( "[:" and ":]" ) delimiters.

    The following character class expressions shall be supported in all locales:

    [:alnum:]   [:cntrl:]   [:lower:]   [:space:]
    [:alpha:]   [:digit:]   [:print:]   [:upper:]
    [:blank:]   [:graph:]   [:punct:]   [:xdigit:]
    
    

    In addition, character class expressions of the form:

    [:name:]
    
    

    are recognized in those locales where the name keyword has been given a charclass definition in the LC_CTYPE category.

  7. In the POSIX locale, a range expression represents the set of collating elements that fall between two elements in the collation sequence, inclusive. In other locales, a range expression has unspecified behavior: strictly conforming applications shall not rely on whether the range expression is valid, or on the set of collating elements matched. A range expression shall be expressed as the starting point and the ending point separated by a hyphen ( '-' ).

    In the following, all examples assume the POSIX locale.

    The starting range point and the ending range point shall be a collating element or collating symbol. An equivalence class expression used as a starting or ending point of a range expression produces unspecified results. An equivalence class can be used portably within a bracket expression, but only outside the range. If the represented set of collating elements is empty, it is unspecified whether the expression matches nothing, or is treated as invalid.

    The interpretation of range expressions where the ending range point is also the starting range point of a subsequent range expression (for example, "[a-m-o]" ) is undefined.

    The hyphen character shall be treated as itself if it occurs first (after an initial '^', if any) or last in the list, or as an ending range point in a range expression. As examples, the expressions "[-ac]" and "[ac-]" are equivalent and match any of the characters 'a', 'c', or '-' ; "[^-ac]" and "[^ac-]" are equivalent and match any characters except 'a', 'c', or '-' ; the expression "[%--]" matches any of the characters between '%' and '-' inclusive; the expression "[--@]" matches any of the characters between '-' and '@' inclusive; and the expression "[a--@]" is either invalid or equivalent to '@', because the letter 'a' follows the symbol '-' in the POSIX locale. To use a hyphen as the starting range point, it shall either come first in the bracket expression or be specified as a collating symbol; for example, "[][.-.]-0]", which matches either a right bracket or any character or collating element that collates between hyphen and 0, inclusive.

    If a bracket expression specifies both '-' and ']', the ']' shall be placed first (after the '^', if any) and the '-' last within the bracket expression.

9.3.6 BREs Matching Multiple Characters

The following rules can be used to construct BREs matching multiple characters from BREs matching a single character:

  1. The concatenation of BREs shall match the concatenation of the strings matched by each component of the BRE.

  2. A subexpression can be defined within a BRE by enclosing it between the character pairs "\(" and "\)". Such a subexpression shall match whatever it would have matched without the "\(" and "\)", except that anchoring within subexpressions is optional behavior; see BRE Expression Anchoring. Subexpressions can be arbitrarily nested.

  3. The back-reference expression '\n' shall match the same (possibly empty) string of characters as was matched by a subexpression enclosed between "\(" and "\)" preceding the '\n'. The character 'n' shall be a digit from 1 through 9, specifying the nth subexpression (the one that begins with the nth "\(" from the beginning of the pattern and ends with the corresponding paired "\)" ). The expression is invalid if less than n subexpressions precede the '\n'. For example, the expression "\(.*\)\1$" matches a line consisting of two adjacent appearances of the same string, and the expression "\(a\)*\1" fails to match 'a'. When the referenced subexpression matched more than one string, the back-referenced expression shall refer to the last matched string. If the subexpression referenced by the back-reference matches more than one string because of an asterisk ( '*' ) or an interval expression (see item (5)), the back-reference shall match the last (rightmost) of these strings.

  4. When a BRE matching a single character, a subexpression, or a back-reference is followed by the special character asterisk ( '*' ), together with that asterisk it shall match what zero or more consecutive occurrences of the BRE would match. For example, "[ab]*" and "[ab][ab]" are equivalent when matching the string "ab".

  5. When a BRE matching a single character, a subexpression, or a back-reference is followed by an interval expression of the format "\{m\}", "\{m,\}", or "\{m,n\}", together with that interval expression it shall match what repeated consecutive occurrences of the BRE would match. The values of m and n are decimal integers in the range 0 <= m<= n<= {RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression "\{m\}" shall match exactly m occurrences of the preceding BRE, "\{m,\}" shall match at least m occurrences, and "\{m,n\}" shall match any number of occurrences between m and n, inclusive.

    For example, in the string "abababccccccd" the BRE "c\{3\}" is matched by characters seven to nine, the BRE "\(ab\)\{4,\}" is not matched at all, and the BRE "c\{1,3\}d" is matched by characters ten to thirteen.

The behavior of multiple adjacent duplication symbols ( '*' and intervals) produces undefined results.

A subexpression repeated by an asterisk ( '*' ) or an interval expression shall not match a null expression unless this is the only match for the repetition or it is necessary to satisfy the exact or minimum number of occurrences for the interval expression.

9.3.7 BRE Precedence

The order of precedence shall be as shown in the following table:

BRE Precedence (from high to low)

Collation-related bracket symbols

[==] [::] [..]

Escaped characters

\<special character>

Bracket expression

[]

Subexpressions/back-references

\(\) \n

Single-character-BRE duplication

* \{m,n\}

Concatenation

 

Anchoring

^ $

9.3.8 BRE Expression Anchoring

A BRE can be limited to matching strings that begin or end a line; this is called "anchoring". The circumflex and dollar sign special characters shall be considered BRE anchors in the following contexts:

  1. A circumflex ( '^' ) shall be an anchor when used as the first character of an entire BRE. The implementation may treat the circumflex as an anchor when used as the first character of a subexpression. The circumflex shall anchor the expression (or optionally subexpression) to the beginning of a string; only sequences starting at the first character of a string shall be matched by the BRE. For example, the BRE "^ab" matches "ab" in the string "abcdef", but fails to match in the string "cdefab". The BRE "\(^ab\)" may match the former string. A portable BRE shall escape a leading circumflex in a subexpression to match a literal circumflex.

  2. A dollar sign ( '$' ) shall be an anchor when used as the last character of an entire BRE. The implementation may treat a dollar sign as an anchor when used as the last character of a subexpression. The dollar sign shall anchor the expression (or optionally subexpression) to the end of the string being matched; the dollar sign can be said to match the end-of-string following the last character.

  3. A BRE anchored by both '^' and '$' shall match only an entire string. For example, the BRE "^abcdef$" matches strings consisting only of "abcdef".

9.4 Extended Regular Expressions

The extended regular expression (ERE) notation and construction rules shall apply to utilities defined as using extended regular expressions; any exceptions to the following rules are noted in the descriptions of the specific utilities using EREs.

9.4.1 EREs Matching a Single Character or Collating Element

An ERE ordinary character, a special character preceded by a backslash, or a period shall match a single character. A bracket expression shall match a single character or a single collating element. An ERE matching a single character enclosed in parentheses shall match the same as the ERE without parentheses would have matched.

9.4.2 ERE Ordinary Characters

An ordinary character is an ERE that matches itself. An ordinary character is any character in the supported character set, except for the ERE special characters listed in ERE Special Characters. The interpretation of an ordinary character preceded by a backslash ( '\' ) is undefined.

9.4.3 ERE Special Characters

An ERE special character has special properties in certain contexts. Outside those contexts, or when preceded by a backslash, such a character shall be an ERE that matches the special character itself. The extended regular expression special characters and the contexts in which they shall have their special meaning are as follows:

.[\(
The period, left-bracket, backslash, and left-parenthesis shall be special except when used in a bracket expression (see RE Bracket Expression). Outside a bracket expression, a left-parenthesis immediately followed by a right-parenthesis produces undefined results.
)
The right-parenthesis shall be special when matched with a preceding left-parenthesis, both outside a bracket expression.
*+?{
The asterisk, plus-sign, question-mark, and left-brace shall be special except when used in a bracket expression (see RE Bracket Expression). Any of the following uses produce undefined results:
|
The vertical-line is special except when used in a bracket expression (see RE Bracket Expression). A vertical-line appearing first or last in an ERE, or immediately following a vertical-line or a left-parenthesis, or immediately preceding a right-parenthesis, produces undefined results.
^
The circumflex shall be special when used as:
$
The dollar sign shall be special when used as an anchor.

9.4.4 Periods in EREs

A period ( '.' ), when used outside a bracket expression, is an ERE that shall match any character in the supported character set except NUL.

9.4.5 ERE Bracket Expression

The rules for ERE Bracket Expressions are the same as for Basic Regular Expressions; see RE Bracket Expression.

9.4.6 EREs Matching Multiple Characters

The following rules shall be used to construct EREs matching multiple characters from EREs matching a single character:

  1. A concatenation of EREs shall match the concatenation of the character sequences matched by each component of the ERE. A concatenation of EREs enclosed in parentheses shall match whatever the concatenation without the parentheses matches. For example, both the ERE "cd" and the ERE "(cd)" are matched by the third and fourth character of the string "abcdefabcdef".

  2. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character plus-sign ( '+' ), together with that plus-sign it shall match what one or more consecutive occurrences of the ERE would match. For example, the ERE "b+(bc)" matches the fourth to seventh characters in the string "acabbbcde". And, "[ab]+" and "[ab][ab]*" are equivalent.

  3. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character asterisk ( '*' ), together with that asterisk it shall match what zero or more consecutive occurrences of the ERE would match. For example, the ERE "b*c" matches the first character in the string "cabbbcde", and the ERE "b*cd" matches the third to seventh characters in the string "cabbbcdebbbbbbcdbc". And, "[ab]*" and "[ab][ab]" are equivalent when matching the string "ab".

  4. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character question-mark ( '?' ), together with that question-mark it shall match what zero or one consecutive occurrences of the ERE would match. For example, the ERE "b?c" matches the second character in the string "acabbbcde".

  5. When an ERE matching a single character or an ERE enclosed in parentheses is followed by an interval expression of the format "{m}", "{m,}", or "{m,n}", together with that interval expression it shall match what repeated consecutive occurrences of the ERE would match. The values of m and n are decimal integers in the range 0 <= m<= n<= {RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression "{m}" matches exactly m occurrences of the preceding ERE, "{m,}" matches at least m occurrences, and "{m,n}" matches any number of occurrences between m and n, inclusive.

    For example, in the string "abababccccccd" the ERE "c{3}" is matched by characters seven to nine and the ERE "(ab){2,}" is matched by characters one to six.

The behavior of multiple adjacent duplication symbols ( '+', '*', '?', and intervals) produces undefined results.

An ERE matching a single character repeated by an '*', '?', or an interval expression shall not match a null expression unless this is the only match for the repetition or it is necessary to satisfy the exact or minimum number of occurrences for the interval expression.

9.4.7 ERE Alternation

Two EREs separated by the special character vertical-line ( '|' ) shall match a string that is matched by either. For example, the ERE "a((bc)|d)" matches the string "abc" and the string "ad". Single characters, or expressions matching single characters, separated by the vertical bar and enclosed in parentheses, shall be treated as an ERE matching a single character.

9.4.8 ERE Precedence

The order of precedence shall be as shown in the following table:

ERE Precedence (from high to low)

Collation-related bracket symbols

[==] [::] [..]

Escaped characters

\<special character>

Bracket expression

[]

Grouping

()

Single-character-ERE duplication

* + ? {m,n}

Concatenation

 

Anchoring

^ $

Alternation

|

For example, the ERE "abba|cde" matches either the string "abba" or the string "cde" (rather than the string "abbade" or "abbcde", because concatenation has a higher order of precedence than alternation).

9.4.9 ERE Expression Anchoring

An ERE can be limited to matching strings that begin or end a line; this is called "anchoring". The circumflex and dollar sign special characters shall be considered ERE anchors when used anywhere outside a bracket expression. This shall have the following effects:

  1. A circumflex ( '^' ) outside a bracket expression shall anchor the expression or subexpression it begins to the beginning of a string; such an expression or subexpression can match only a sequence starting at the first character of a string. For example, the EREs "^ab" and "(^ab)" match "ab" in the string "abcdef", but fail to match in the string "cdefab", and the ERE "a^b" is valid, but can never match because the 'a' prevents the expression "^b" from matching starting at the first character.

  2. A dollar sign ( '$' ) outside a bracket expression shall anchor the expression or subexpression it ends to the end of a string; such an expression or subexpression can match only a sequence ending at the last character of a string. For example, the EREs "ef$" and "(ef$)" match "ef" in the string "abcdef", but fail to match in the string "cdefab", and the ERE "e$f" is valid, but can never match because the 'f' prevents the expression "e$" from matching ending at the last character.

9.5 Regular Expression Grammar

Grammars describing the syntax of both basic and extended regular expressions are presented in this section. The grammar takes precedence over the text. See the Shell and Utilities volume of IEEE Std 1003.1-2001, Section 1.10, Grammar Conventions.

9.5.1 BRE/ERE Grammar Lexical Conventions

The lexical conventions for regular expressions are as described in this section.

Except as noted, the longest possible token or delimiter beginning at a given point is recognized.

The following tokens are processed (in addition to those string constants shown in the grammar):

COLL_ELEM_SINGLE
Any single-character collating element, unless it is a META_CHAR.
COLL_ELEM_MULTI
Any multi-character collating element.
BACKREF
Applicable only to basic regular expressions. The character string consisting of '\' followed by a single-digit numeral, '1' to '9'.
DUP_COUNT
Represents a numeric constant. It shall be an integer in the range 0 <= DUP_COUNT <= {RE_DUP_MAX}. This token is only recognized when the context of the grammar requires it. At all other times, digits not preceded by '\' are treated as ORD_CHAR.
META_CHAR
One of the characters:
^
When found first in a bracket expression
-
When found anywhere but first (after an initial '^', if any) or last in a bracket expression, or as the ending range point in a range expression
]
When found anywhere but first (after an initial '^', if any) in a bracket expression
L_ANCHOR
Applicable only to basic regular expressions. The character '^' when it appears as the first character of a basic regular expression and when not QUOTED_CHAR. The '^' may be recognized as an anchor elsewhere; see BRE Expression Anchoring.
ORD_CHAR
A character, other than one of the special characters in SPEC_CHAR.
QUOTED_CHAR
In a BRE, one of the character sequences:
\^    \.   \*    \[    \$    \\

In an ERE, one of the character sequences:

\^    \.    \[    \$    \(    \)    \|
\*    \+    \?    \{    \\

R_ANCHOR
(Applicable only to basic regular expressions.) The character '$' when it appears as the last character of a basic regular expression and when not QUOTED_CHAR. The '$' may be recognized as an anchor elsewhere; see BRE Expression Anchoring.
SPEC_CHAR
For basic regular expressions, one of the following special characters:
.
Anywhere outside bracket expressions
\
Anywhere outside bracket expressions
[
Anywhere outside bracket expressions
^
When used as an anchor (see BRE Expression Anchoring) or when first in a bracket expression
$
When used as an anchor
*
Anywhere except first in an entire RE, anywhere in a bracket expression, directly following "\(", directly following an anchoring '^'

For extended regular expressions, shall be one of the following special characters found anywhere outside bracket expressions:

^    .    [    $    (    )    |
*    +    ?    {    \

(The close-parenthesis shall be considered special in this context only if matched with a preceding open-parenthesis.)

9.5.2 RE and Bracket Expression Grammar

This section presents the grammar for basic regular expressions, including the bracket expression grammar that is common to both BREs and EREs.

%token    ORD_CHAR QUOTED_CHAR DUP_COUNT

%token BACKREF L_ANCHOR R_ANCHOR
%token Back_open_paren Back_close_paren /* '\(' '\)' */
%token Back_open_brace Back_close_brace /* '\{' '\}' */
/* The following tokens are for the Bracket Expression grammar common to both REs and EREs. */
%token COLL_ELEM_SINGLE COLL_ELEM_MULTI META_CHAR
%token Open_equal Equal_close Open_dot Dot_close Open_colon Colon_close /* '[=' '=]' '[.' '.]' '[:' ':]' */
%token class_name /* class_name is a keyword to the LC_CTYPE locale category */ /* (representing a character class) in the current locale */ /* and is only recognized between [: and :] */
%start basic_reg_exp %%
/* -------------------------------------------- Basic Regular Expression -------------------------------------------- */ basic_reg_exp : RE_expression | L_ANCHOR | R_ANCHOR | L_ANCHOR R_ANCHOR | L_ANCHOR RE_expression | RE_expression R_ANCHOR | L_ANCHOR RE_expression R_ANCHOR ; RE_expression : simple_RE | RE_expression simple_RE ; simple_RE : nondupl_RE | nondupl_RE RE_dupl_symbol ; nondupl_RE : one_char_or_coll_elem_RE | Back_open_paren RE_expression Back_close_paren | BACKREF ; one_char_or_coll_elem_RE : ORD_CHAR | QUOTED_CHAR | '.' | bracket_expression ; RE_dupl_symbol : '*' | Back_open_brace DUP_COUNT Back_close_brace | Back_open_brace DUP_COUNT ',' Back_close_brace | Back_open_brace DUP_COUNT ',' DUP_COUNT Back_close_brace ;
/* -------------------------------------------- Bracket Expression ------------------------------------------- */ bracket_expression : '[' matching_list ']' | '[' nonmatching_list ']' ; matching_list : bracket_list ; nonmatching_list : '^' bracket_list ; bracket_list : follow_list | follow_list '-' ; follow_list : expression_term | follow_list expression_term ; expression_term : single_expression | range_expression ; single_expression : end_range | character_class | equivalence_class ; range_expression : start_range end_range | start_range '-' ; start_range : end_range '-' ; end_range : COLL_ELEM_SINGLE | collating_symbol ; collating_symbol : Open_dot COLL_ELEM_SINGLE Dot_close | Open_dot COLL_ELEM_MULTI Dot_close | Open_dot META_CHAR Dot_close ; equivalence_class : Open_equal COLL_ELEM_SINGLE Equal_close | Open_equal COLL_ELEM_MULTI Equal_close ; character_class : Open_colon class_name Colon_close ;

The BRE grammar does not permit L_ANCHOR or R_ANCHOR inside "\(" and "\)" (which implies that '^' and '$' are ordinary characters). This reflects the semantic limits on the application, as noted in BRE Expression Anchoring. Implementations are permitted to extend the language to interpret '^' and '$' as anchors in these locations, and as such, conforming applications cannot use unescaped '^' and '$' in positions inside "\(" and "\)" that might be interpreted as anchors.

9.5.3 ERE Grammar

This section presents the grammar for extended regular expressions, excluding the bracket expression grammar.

Note:
The bracket expression grammar and the associated %token lines are identical between BREs and EREs. It has been omitted from the ERE section to avoid unnecessary editorial duplication.
%token  ORD_CHAR QUOTED_CHAR DUP_COUNT
%start  extended_reg_exp
%%

/* -------------------------------------------- Extended Regular Expression -------------------------------------------- */ extended_reg_exp : ERE_branch | extended_reg_exp '|' ERE_branch ; ERE_branch : ERE_expression | ERE_branch ERE_expression ; ERE_expression : one_char_or_coll_elem_ERE | '^' | '$' | '(' extended_reg_exp ')' | ERE_expression ERE_dupl_symbol ; one_char_or_coll_elem_ERE : ORD_CHAR | QUOTED_CHAR | '.' | bracket_expression ; ERE_dupl_symbol : '*' | '+' | '?' | '{' DUP_COUNT '}' | '{' DUP_COUNT ',' '}' | '{' DUP_COUNT ',' DUP_COUNT '}' ;

The ERE grammar does not permit several constructs that previous sections specify as having undefined results:

Implementations are permitted to extend the language to allow these. Conforming applications cannot use such constructs.


UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
[ Main Index | XBD | XCU | XSH | XRAT ]

肝脏低密度影是什么意思 ABB式的词语有什么 倒春寒是什么意思 迷津是什么意思 心率过快是什么原因
胸闷什么感觉 头发麻是什么病的前兆 化妆品属于什么行业 balea是什么牌子 梦见车丢了是什么意思
强盗是什么意思 肌桥是什么意思 7到9点是什么时辰 什么的长城 喷塑工是干什么的
无以言表是什么意思 鲁迅原名什么 idh是什么意思 杏仁治什么病 尿液弱阳性什么意思
什么秀丽cj623037.com 维生素C起什么作用hcv8jop4ns2r.cn 拉肚子喝什么饮料hcv7jop9ns7r.cn 孕妇吃什么能马上通便hcv9jop4ns8r.cn 大运是什么jinxinzhichuang.com
托班是什么意思hcv8jop4ns3r.cn 女团ace是什么意思hcv9jop4ns8r.cn 吃什么排出全身毒素hcv8jop2ns0r.cn 地奥司明片治疗什么病jasonfriends.com 六角龙鱼吃什么hcv9jop1ns4r.cn
地铁站务员是干什么的hcv8jop8ns9r.cn 全身皮肤瘙痒是什么原因引起的hcv8jop2ns9r.cn 眉毛长痘是什么原因hebeidezhi.com 918是什么星座hcv8jop4ns5r.cn 夫人是什么意思hcv8jop6ns8r.cn
结婚10年是什么婚hcv9jop1ns5r.cn 藿香正气水有什么用hcv9jop8ns2r.cn 退烧吃什么药hcv7jop9ns4r.cn 暗经是什么意思hcv9jop6ns8r.cn 难产是什么意思hcv9jop7ns1r.cn
百度