在matlab中使用regexp验证并剪切字符串
我有以下字符串:在matlab中使用regexp验证并剪切字符串,regex,matlab,Regex,Matlab,我有以下字符串: {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921} 我想验证字符串的格式,即单词“variable”是第二个单词,我想在第三个字符串(在本例中为“D_foo”)的最后一个“/”之后检索字符串 我怎样才能验证这一点,并检索我搜索的刺 我尝试了以下方法: regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
我想验证字符串的格式,即单词“variable”是第二个单词,我想在第三个字符串(在本例中为“D_foo”)的最后一个“/”之后检索字符串
我怎样才能验证这一点,并检索我搜索的刺
我尝试了以下方法:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
无功而返
备注
要分析的字符串在komma之后不会被拆分,这只是由于字符串的长度
编辑
我的字符串是:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
而不是一个细胞,这是可以理解的。我在字符串的开头和结尾添加了sybol'以表示它是一个字符串。要回答问题的第一部分,您可以这样写:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
对于第二部分,您可以执行以下操作:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.
我知道您在问题中提到使用regexp,但我不确定这是否是一项要求?如果可以接受其他解决方案,您可以尝试以下方法:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
不,我不需要处理regexp。我希望能够用两行代码而不是4行代码来解决这个问题。(验证和拆分)。尽管如此,你的解决方案还是解决了我的问题,对此我表示感谢。