How to split string on the last delimiter?
19 次查看(过去 30 天)
显示 更早的评论
I am supposed to separate a string 'abcdef@eng.abc.edu' into three separate string 'abcdef', 'eng.abc', 'edu' using strsplit function. However, I don't know how to split the string at the last . delimiter instead of separate them into four strings using C = strsplit('abcdef@eng.abc.edu',{'@','.'}). Please help, thanks!
1 个评论
dpb
2016-11-26
Well, you've got to have some other rule that tells you to not consider one delimiter a delimiter at all...what is that rule? You'd have to start by separating out the domain from the address then parse off the last TLD (top-level domain) by searching for the last dot.
采纳的回答
Walter Roberson
2016-11-26
lastdot_pos = find(YourString == '.', 1, 'last');
3 个评论
Walter Roberson
2016-11-27
YourString = 'abcdef@eng.abc.edu';
lastdot_pos = find(YourString == '.', 1, 'last');
part12 = YourString(1 : lastdot_pos - 1);
part12cell = strsplit(part12, '@');
part1 = part12cell{1};
part2 = part12cell{2};
part3 = YourString(lastdot_pos+1 : end );
Alternative:
YourString = 'abcdef@eng.abc.edu';
part123cell = regexp(YourString, '@|\.(?=\w+$)', 'split');
part1 = part123cell{1};
part2 = part123cell{2};
part3 = part123cell{3};
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!