필터 지우기
필터 지우기

I need help in the code. what am I doing wrong?

조회 수: 1 (최근 30일)
Manav Divekar
Manav Divekar 2021년 11월 23일
답변: Johan 2021년 11월 23일
I am writing a function to display host name from the url. This function should return only hostname without the port number
eg
Output: en.wikipedia.org
or
My code so far
function [result] = url2hostname(url)
c1 = strfind(url,'//');
ind1 = strfind(url,'/');
if isempty(c1) && isempty(ind1)
result = url;
return;
end
if ~isempty(c1)
if numel(ind1)>2
result = url(1:ind1(3)-1);
else
result = url;
end
else
result = url(1:ind1(1)-1);
end
return;
result = result;

채택된 답변

Johan
Johan 2021년 11월 23일
Hi,
I think you can just add a check for a ':' character in your code. Alternatively you can also use regular expression to do this kind of string manipulation. I put two exemples below:
1) The expression string tells regexp to match
any number of alphanumerical character: \w+
but only after it finds a :// character: (?<=://)
and only until it finds either a : or a / ?([^:]|/)+
expression = '(?<=://)\w+.?([^:]|/)+';
url = 'https://en.wikipedia.org:443/wiki/Kitten?printable=yes&download=no#External_links';
regexp(url,expression,'match')
ans = 1×1 cell array
{'en.wikipedia.org'}
url = 'http://www.google.com';
regexp(url,expression,'match')
ans = 1×1 cell array
{'www.google.com'}
2) Remake your code with simpler form of regexp to isolate the hostname of the url
url2hostname('http://www.google.com')
ans = 'www.google.com'
url2hostname('https://en.wikipedia.org:443/wiki/Kitten?printable=yes&download=no#External_links')
ans = 'en.wikipedia.org'
function result = url2hostname(url)
tag = regexp(url,'//+');
if ~isempty(tag)
url = url(tag+2:end);
end
tag = regexp(url,'/','once');
if ~isempty(tag)
url = url(1:regexp(url,'./','once'));
end
tag = regexp(url,'.:','once');
if ~isempty(tag)
url = url(1:tag);
end
result = url;
end

추가 답변 (1개)

Ive J
Ive J 2021년 11월 23일
This may help:
fnc = @(url) regexp(url, "(?<=https?://)(.*?)[^:]*", 'match');
fnc('https://en.wikipedia.org:443/wiki/Kitten?printable=yes&download=no#External_links')
ans = 1×1 cell array
{'en.wikipedia.org'}
fnc('http://www.google.com')
ans = 1×1 cell array
{'www.google.com'}

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by