2016-07-06 75 views
0

我有一個變量,讓我們把它叫做test = data.measurement.info;正則表達式中的變量

現在我想成立一​​個正則表達式所以不是測量它會匹配任何東西。

我知道點(匹配任何東西)可以使用(。),但我如何將它包含在變量中?

+0

所以'數據。*。info'? – GameOfThrows

+0

不幸的是,這給了:'。'解析錯誤。用法可能是無效的MATLAB語法。 –

回答

1

所以,假設你有名稱的列表:

test = {'data.measurement.info','data.123.info','data.measurement.123'}; 
expression = 'data+\.+\w*+\.+info'; % \w* is any alpha numeric word; alternatively, if you just want anything, use a single . like 'data+\.+.+\.+info' 

regexp(test,expression,'match') 

ans = 

    {1x1 cell} {1x1 cell} {} 
+0

它也匹配例如'dataaaaaaa.measurement.info'' :) – DVarga

0

下面的正則表達式匹配以下內容: 「」

「數據」 +只有一個字符+至少一個單詞字符+正好一個「。」字符+信息

testCell = { 
    'data..measurement.info', ... % double "." is illegal 
    'data.custommeasurement.info', ... % okay 
    'data.123.info', ... % okay 
    'data..info', ... % empty second part is illegal 
    'data.measurement.123'}; % not okay 

% data + exactly one "." + at least one word character + exactly one "." + info 
expression = 'data\.{1}\w+\.{1}info'; 

regexp(testCell, expression, 'match') 

輸出

ans = {} {1x1 cell} {1x1 cell} {} {}