2017-01-17 86 views
1

之間凸顯我試圖從東西像提取模式{THIS_PATTERN}類似如下:匹配資金和大括號

.intro-header { 
    padding-top: 50px; /* If you're making other pages, make sure there is 50px of padding to make sure the navbar doesn't overlap content! */ 
    padding-bottom: 50px; 
    text-align: center; 
    color: #f8f8f8; 
    background: url(../img/{BG_IMAGE_0}) no-repeat center center; 
    background-size: cover; 
} 

在這種情況下,將{BG_IMAGE_0}

我想不出有什麼錯我的正則表達式:\{[A-Z_]*\}

我有一個regex101小提琴這裏: https://regex101.com/r/KF5Sz6/2

+1

您錯過了「數字」'\ {[A-Z_ \ d] * \}' – strah

回答

3

這應該工作:

\{[A-Z_0-9]*?\} 

你似乎沒有考慮「0」。

通過這樣做它像這樣*?,你也使它非貪婪,應防止來自其他{}干擾,在同一行,是你在你未來的項目在任何絆倒。

+1

您不需要轉義括號({和}),也可以使用\ w而不是[A-Z_0 -9] – Maslo

+0

你說得對,我實際上試圖儘量保持它儘可能接近OP代碼,假設可能還有其他未提及的限制,比如只有大寫字母,數字和_。但是'{\ w +?}'可以工作得很好。 – TaoStyle