Function to extract certain elements from a character array
2 次查看(过去 30 天)
显示 更早的评论
function [user_name,domain_name] = GetUserAndDomain(email) user_name=email(1:strfind(email,'@')-1); domain_name=email(strfind(email,'.')+1:end); end
Basically if my email is blah@gmail.com, my user_name should return 'blah', and my domain_name should return 'com'.
I think my code works fine for the user name. But for the domain name, how do i account for the fact that there might be dots within the user_name, such as blah.96@gmail.com?
回答(1 个)
Stephen23
2014-10-10
编辑:Stephen23
2014-10-10
You could use regexp , allowing you to separate all of the username elements in one go. There is even a complete worked example in the documentation which explains how to develop the regular expression for email addresses:
You can change the regular expression to suit the usernames that your data has, perhaps something like this:
>> A = 'blah.96@gmail.com';
>> regexpi(A,'(.+?)@(.+)\.(.+)','tokens','once')
ans =
'blah.96' 'gmail' 'com'
Note that regexp also accepts a cell array of strings as its input, so you could parse all of the email addresses at once, using just one line of code.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!