首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匹配一个字符串,只要它不出现在另一个字符串中

匹配一个字符串,只要它不出现在另一个字符串中
EN

Stack Overflow用户
提问于 2013-05-23 02:28:19
回答 1查看 65关注 0票数 1

简短问题

有没有可能使用正则表达式来匹配一个没有出现在其他更长字符串中的字符串?(我正在使用MATLAB)

示例

代码语言:javascript
复制
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)将其发布。除了标记函数的语法之外,我还想标记出现在语法之外的函数的任何输入/输出。假设我以某种方式从函数中提取了帮助注释。以下是一些示例注释。

代码语言:javascript
复制
% 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.

在用我当前的方法标记这些注释之后,我有以下内容

代码语言:javascript
复制
%% 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部分中以单行间距显示。我希望最后的文本是

代码语言:javascript
复制
%% 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数组。

代码语言:javascript
复制
syntax = {'x = somefunction(y)', '[out1, out2] = somefunction(in1, in2)'};
inputs = {'y', 'in1', 'in2'};
outputs = {'x', 'out1', 'out2'};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-23 03:39:45

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

代码语言:javascript
复制
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'}';

使用带有前视和后视运算符的正则表达式,即inputsoutputs应该包含在\s,\.\!\?;:%中,前后都是字母数字字符,或者在开头(无用?)或文本末尾:

代码语言:javascript
复制
expr = strcat('(?<=\w[\s,\.\!\?;:%]+|^)', inout,'(?=[\s,\.\!\?;:]+\w|\.?$)');
regexprep(txt,expr,'|$&|')

结果是:

代码语言:javascript
复制
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|.

另一个选择

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16699108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档