2017-01-17 83 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