2016-07-06 79 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} {} {}