Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Matlab能否消除URL中的路径,只留下域部分?_String_Matlab_Url_Path_Domain Name - Fatal编程技术网

String Matlab能否消除URL中的路径,只留下域部分?

String Matlab能否消除URL中的路径,只留下域部分?,string,matlab,url,path,domain-name,String,Matlab,Url,Path,Domain Name,Matlab能消除URL中的路径并只留下域部分吗?Matlab是否有任何功能来消除后面的路径 例如,示例1: input :http://www.mathworks.com/help/images/removing-noise-from-images.html output :http://www.mathworks.com 代码: 示例运行: %% Long URLs with extensions disp(domain_name('www.mathworks.com/help/im

Matlab能消除URL中的路径并只留下域部分吗?Matlab是否有任何功能来消除后面的路径

例如,示例1:

 input  :http://www.mathworks.com/help/images/removing-noise-from-images.html
 output :http://www.mathworks.com
代码:

示例运行:

%% Long URLs with extensions
disp(domain_name('www.mathworks.com/help/images/removing-noise-from-images.html'))
disp(domain_name('http://www.mathworks.com/help/images/removing-noise-from-images.html'))

%% Short URLs without HTTP://
disp(domain_name('www.mathworks.com'))
disp(domain_name('www.mathworks.com/'))

%% Short URLs with HTTP://
disp(domain_name('http://www.mathworks.com'))
disp(domain_name('http://www.mathworks.com/'))
返回:

www.mathworks.com
http://www.mathworks.com
www.mathworks.com
www.mathworks.com
http://www.mathworks.com
http://www.mathworks.com
另一种可能也是有效的方法是使用数字,但显然我更喜欢数字

编辑1:如果您喜欢在同一时间使用大量URL,可以使用单元格数组。显然,输出也将是一个单元格数组。请看下面的MATLAB脚本以了解它-

% Input
in_urls_cell = [{'http://mathworks.com/'},{'mathworks.com/help/matlab/ref/strcmpi.html'},{'mathworks.com/help/matlab/ref/strcmpi@dfvfv.html'}];

% Get domain name
out_urls_cell = cell(size(in_urls_cell));
for count = 1:numel(in_urls_cell)
    out_urls_cell(count)={domain_name(cell2mat(in_urls_cell(count)))};
end

% Display only domain name
for count = 1:numel(out_urls_cell)
    disp(cell2mat(out_urls_cell(count)));
end  
上面的脚本返回-

http://mathworks.com
mathworks.com
mathworks.com
代码:

示例运行:

%% Long URLs with extensions
disp(domain_name('www.mathworks.com/help/images/removing-noise-from-images.html'))
disp(domain_name('http://www.mathworks.com/help/images/removing-noise-from-images.html'))

%% Short URLs without HTTP://
disp(domain_name('www.mathworks.com'))
disp(domain_name('www.mathworks.com/'))

%% Short URLs with HTTP://
disp(domain_name('http://www.mathworks.com'))
disp(domain_name('http://www.mathworks.com/'))
返回:

www.mathworks.com
http://www.mathworks.com
www.mathworks.com
www.mathworks.com
http://www.mathworks.com
http://www.mathworks.com
另一种可能也是有效的方法是使用数字,但显然我更喜欢数字

编辑1:如果您喜欢在同一时间使用大量URL,可以使用单元格数组。显然,输出也将是一个单元格数组。请看下面的MATLAB脚本以了解它-

% Input
in_urls_cell = [{'http://mathworks.com/'},{'mathworks.com/help/matlab/ref/strcmpi.html'},{'mathworks.com/help/matlab/ref/strcmpi@dfvfv.html'}];

% Get domain name
out_urls_cell = cell(size(in_urls_cell));
for count = 1:numel(in_urls_cell)
    out_urls_cell(count)={domain_name(cell2mat(in_urls_cell(count)))};
end

% Display only domain name
for count = 1:numel(out_urls_cell)
    disp(cell2mat(out_urls_cell(count)));
end  
上面的脚本返回-

http://mathworks.com
mathworks.com
mathworks.com
这种模式应该可以做到:

>> str = 'http://www.mathworks.com/help/images/removing-noise-from-images.html';
>> out = regexp(str,'\w*://[^/]*','match','once')
out = 
    'http://www.mathworks.com'
搜索模式
'\w*://[^/]*'
表示查找以与协议(例如http、https、rtsp)对应的一些“单词”字符(
'\w*
)开头的字符串,然后是无处不在的
://
,然后是任意数量的非正斜杠字符(
[^/]*

编辑:使用
“一次”
选项应消除嵌套的
单元格


更新:仅更新主机名,允许无协议输入。

>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
          'https://www.mathworks.com/help/matlab/ref/strcmpi@dfvfv.html';
          'google.com/voice'}
>> out = regexp(str,'([^/]*)(?=/[^/])','match','once')
out = 
    'www.mathworks.com'
    'www.mathworks.com'
    'google.com'

更新2:
regexp
疯狂

>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
          'https://www.mathworks.com/help/matlab/ref/strcmpi@dfvfv.html';
          'google.com/voice';
          'http://monkey.org/';
          'stackoverflow.com/';
          'meta.stackoverflow.com'};
>> out = regexp(str,'.*?[^/](?=(/([^/]|$)|$))','match','once')
out = 
    'http://www.mathworks.com'
    'https://www.mathworks.com'
    'google.com'
    'http://monkey.org'
    'stackoverflow.com'
    'meta.stackoverflow.com'

这种模式应该可以做到:

>> str = 'http://www.mathworks.com/help/images/removing-noise-from-images.html';
>> out = regexp(str,'\w*://[^/]*','match','once')
out = 
    'http://www.mathworks.com'
搜索模式
'\w*://[^/]*'
表示查找以与协议(例如http、https、rtsp)对应的一些“单词”字符(
'\w*
)开头的字符串,然后是无处不在的
://
,然后是任意数量的非正斜杠字符(
[^/]*

编辑:使用
“一次”
选项应消除嵌套的
单元格


更新:仅更新主机名,允许无协议输入。

>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
          'https://www.mathworks.com/help/matlab/ref/strcmpi@dfvfv.html';
          'google.com/voice'}
>> out = regexp(str,'([^/]*)(?=/[^/])','match','once')
out = 
    'www.mathworks.com'
    'www.mathworks.com'
    'google.com'

更新2:
regexp
疯狂

>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
          'https://www.mathworks.com/help/matlab/ref/strcmpi@dfvfv.html';
          'google.com/voice';
          'http://monkey.org/';
          'stackoverflow.com/';
          'meta.stackoverflow.com'};
>> out = regexp(str,'.*?[^/](?=(/([^/]|$)|$))','match','once')
out = 
    'http://www.mathworks.com'
    'https://www.mathworks.com'
    'google.com'
    'http://monkey.org'
    'stackoverflow.com'
    'meta.stackoverflow.com'


如果我有很多数据,其中一些以“.my”、“.com”、“.uk”结尾,怎么样。。。。?我能在一次内处理所有这些吗?例如,str1='';str2='';str1(1:strfind(str1,'.com')+3)第二个字符串如何?针对一般情况进行编辑。为什么会出现类似“disp(domain_name('www.mathworks.com/help/images/removing noise from images.html')??未定义的函数或方法“domain_name”,用于“char”类型的输入参数。我将“code”放在“matlab编辑器”中,'示例运行在'命令窗口'中,我有什么做错了吗?是的,您需要将函数作为一个单独的文件写入当前目录,或将其添加到函数文件所在的MATLAB路径。如果我获得了大量数据,其中一些域以'.my'、'.com'、'.uk'结尾,如何。。。。?我能在一次内处理所有这些吗?例如,str1='';str2='';str1(1:strfind(str1,'.com')+3)第二个字符串如何?针对一般情况进行编辑。为什么会出现类似“disp(domain_name('www.mathworks.com/help/images/removing noise from images.html')??未定义的函数或方法“domain_name”,用于“char”类型的输入参数。我将“code”放在“matlab编辑器”中,'示例运行在'命令窗口'中,我有什么做错了吗?是的,您需要将函数作为一个单独的文件写入当前目录,或将其添加到函数文件所在的MATLAB路径中。请尝试将没有'http://'的情况包括在内,使其具有一般性。它返回空单元格,截至现在。str={'','','',};regexp(str,'\w*:/[^/]*','match')如何在matlab编辑器中输入大量数据,而不是在命令窗口中键入?我希望所有的输出都是矩阵形式的[nx1],现在,我得到这样的输出,有很多数据,ans={1x1 cell}{1x1 cell}{1x1 cell}@Divakar,这不是URL。OP要求这样做吗?我想要有大量数据的答案,如下图所示:ans=''''''尝试通过包括没有“http://”的情况使其具有一般性。它返回空单元格,截至现在。str={'','','',};regexp(str,'\w*:/[^/]*','match')如何在matlab编辑器中输入大量数据,而不是在命令窗口中键入?我希望所有的输出都是矩阵形式的[nx1],现在,我得到这样的输出,有很多数据,ans={1x1 cell}{1x1 cell}{1x1 cell}@Divakar,这不是URL。OP有问这个吗?我想要一个有大量数据的答案,如下所示:ans=''''