简短问题
有没有可能使用正则表达式来匹配一个没有出现在其他更长字符串中的字符串?(我正在使用MATLAB)
示例
shortString = {'hello', 'there'};
longString = {'why hello there', 'hello, my friend', 'hello and goodbye'};
wholeString = ['"why hello there", said one guy. The other guy over ' ...
'there said hello, my friend. hello and goodbye, said the ' ...
'third guy. So let''s all say hello!'];在本例中,我希望在wholeString中匹配shortString的元素,只要它们不作为longString元素的子字符串出现在wholeString中。
在上面的示例中,我只想匹配来自'So let''s all say hello!'的'hello'和来自'The other guy over there'的'there'。
具体问题
我正在编写一个函数,它将在函数的顶部标记帮助注释,以便使用MATLAB的publish函数(请参阅Publishing Markup)将其发布。除了标记函数的语法之外,我还想标记出现在语法之外的函数的任何输入/输出。假设我以某种方式从函数中提取了帮助注释。以下是一些示例注释。
% SYNTAX
% x = somefunction(y)
% [out1, out2] = somefunction(in1, in2)
%
% DESCRIPTION
% x = somefunction(y) does something to y and returns x. I also dont wan't
% your answer to match the 'y' in 'your' or ''y''.
%
% [out1, out2] = somefunction(in1, in2) does something else with in1 and
% in2. Then it returns out1 and out2.在用我当前的方法标记这些注释之后,我有以下内容
%% Syntax
% x = somefunction(y)
% [out1, out2] = somefunction(in1, in2)
%
%% Description
% |x = somefunction(y)| does something to y and returns x. I also dont wan't
% your answer to match the 'y' in your or ''y''.
%
% |[out1, out2] = somefunction(in1, in2)| does something else with in1 and
% in2. Then it returns out1 and out2.我想让每个输入和输出参数在Description部分中以单行间距显示。我希望最后的文本是
%% Syntax
% x = somefunction(y)
% [out1, out2] = somefunction(in1, in2)
%
%% Description
% |x = somefunction(y)| does something to |y| and returns |x|. I also dont wan't
% your answer to match the 'y' in 'your', or ''y''.
%
% |[out1, out2] = somefunction(in1, in2)| does something else with |in1| and
% |in2|. Then it returns |out1| and |out2|.问题是我不能仅仅匹配输入参数,或者我也会在已经是单空格的语法部分中匹配它们。
我可以使用一个包含每个语法定义的cell-array,一个包含每个输入参数的cell-array,以及一个包含每个输出参数的cell数组。
syntax = {'x = somefunction(y)', '[out1, out2] = somefunction(in1, in2)'};
inputs = {'y', 'in1', 'in2'};
outputs = {'x', 'out1', 'out2'};发布于 2013-05-23 03:39:45
这有点特别,我认为它可能会改进,但假设您的输入是:
txt = ['% SYNTAX' char(13)...
'% x = somefunction(y)' char(13)...
'% [out1, out2] = somefunction(in1, in2)' char(13)...
'%' char(13)...
'% DESCRIPTION' char(13)...
'% x = somefunction(y) does something to y and returns x. I also dont wan''t' char(13)...
'% your answer to match the ''y'' in ''your'' or ''''y''''.' char(13)...
'%' char(13)...
'% [out1, out2] = somefunction(in1, in2) does something else with in1 and ' char(13)...
'% in2. Then it returns out1 and out2.'];
inout = {'y', 'in1', 'in2', 'x', 'out1', 'out2'}';使用带有前视和后视运算符的正则表达式,即inputs和outputs应该包含在\s,\.\!\?;:%中,前后都是字母数字字符,或者在开头(无用?)或文本末尾:
expr = strcat('(?<=\w[\s,\.\!\?;:%]+|^)', inout,'(?=[\s,\.\!\?;:]+\w|\.?$)');
regexprep(txt,expr,'|$&|')结果是:
ans =
% SYNTAX
% x = somefunction(y)
% [out1, out2] = somefunction(in1, in2)
%
% DESCRIPTION
% x = somefunction(y) does something to |y| and returns |x|. I also dont wan't
% your answer to match the 'y' in 'your' or ''y''.
%
% [out1, out2] = somefunction(in1, in2) does something else with |in1| and
% |in2|. Then it returns |out1| and |out2|.另一个选择
只需几个步骤,您就可以匹配syntax的start和end位置,然后检索inputs/outputs的位置并从替换操作中排除start/pos中的那些位置。
https://stackoverflow.com/questions/16699108
复制相似问题