1

简短的问题

是否可以使用正则表达式来匹配未出现在其他较长字符串中的字符串?(我正在使用 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!'];

在此示例中,我想匹配shortStringin的元素wholeString,只要它们不wholeString作为 的元素的子字符串出现在longString

在上面的例子中,我只想匹配'hello'from'So let''s all say hello!''there'from 'The other guy over there'

具体问题

我正在编写一个函数,该函数将标记函数顶部的帮助注释,以准备使用 MATLABpublish函数发布它(请参阅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.

我想让每个输入和输出参数在“描述”部分中显示为等距。我希望最终文本是

%% 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)'};
inputs = {'y', 'in1', 'in2'};
outputs = {'x', 'out1', 'out2'};
4

1 回答 1

1

这有点临时,我认为它可能会有所改进,但假设您的输入是:

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'}';

使用带有前瞻和后视运算符的正则表达式,即你的inputsandoutputs应该包含在\s,\.\!\?;:%AND 之前,并且后跟字母数字字符,或者在文本的开头(没用?)或结尾:

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|.

或者

只需几个步骤,您就可以匹配startend的位置syntax,然后检索 的位置inputs/outputs并从替换操作中排除start/pos.

于 2013-05-22T19:39:44.630 回答