2017-01-10 112 views

回答

4

在正則表達式$通常在線路的末端匹配空字符串,但在現實中,如果$在特定的地方出現在正則表達式(例如,在正則表達式的結束,或在年底,這是唯一的真一個小組,IIRC)。如果$出現在「中間」,它只與$字符匹配。同樣適用於^。例如。 (string-match "a^b$c" "1a^b$c2")返回1

C-H我克(emacs) Regexps文件此行爲:

‘^’ 
    is a special character that matches the empty string, but only at 
    the beginning of a line in the text being matched. Otherwise it 
    fails to match anything. Thus, ‘^foo’ matches a ‘foo’ that occurs 
    at the beginning of a line. 

    For historical compatibility reasons, ‘^’ can be used with this 
    meaning only at the beginning of the regular expression, or after 
    ‘\(’ or ‘\|’. 

‘$’ 
    is similar to ‘^’ but matches only at the end of a line. Thus, 
    ‘x+$’ matches a string of one ‘x’ or more at the end of a line. 

    For historical compatibility reasons, ‘$’ can be used with this 
    meaning only at the end of the regular expression, or before ‘\)’ 
    or ‘\|’.