2017-09-15 15 views
1

我有一個配置文件如下:比賽開始或沒有正則表達式

property = 10 
#property = 10 
#this is a test for #property 
#this is a test for property 

我試圖拿出一個正則表達式匹配只有第一兩種情況:

property = 10 //this one 
#property = 10 //and this one 
#this is a test for #property 
#this is a test for property 

到目前爲止,我所做的就是:

#property.*|^property.* 

這是結果:

enter image description here

我試過其他的變種,但結果作最壞的改變:

^#property.*|^property.* 
[^\s]#property.*|^property.* 

下面是我使用的在線正則表達式的鏈接:

https://regex101.com/r/K2tm75/2

我將在grep命令中使用此正則表達式作爲bash腳本的一部分。

+0

有詢問是否標記正則表達式,如果您標記的語言也將是更好的建議。 –

回答

2

你需要確保你在字符串的開始匹配#propertyproperty

我建議

^#?property.* 

或 - 只在最後與=和數字匹配它們:

^#?property *= *[0-9]+$ 

regex demo 1regex demo 2

^#?property.*輸入的開始匹配,則可選的##?比賽1或0的出現,然後property然後任何0+字符)。

第二圖案 - ^#?property *= *[0-9]+$ - 類似的開頭匹配,但是,property後,匹配零個或多個空格,=,零個或多個空格,然後1+數字和字符串的末尾(與$)。

+0

'^#* property'將property'匹配'##以及(或好或壞..?)。 –

+1

@JamesBrown:本'*'量詞匹配零個或多個量化的子模式。我不知道是否應該匹配'####屬性文本...'。如果是,那麼是的,'*'是一個更好的量詞在這裏使用。 –

+0

@WiktorStribiżew你的答案只是讓我認識到大約/克。與該開關完美無瑕地工作。 –

相關問題