Function to extract certain elements from a character array
2 views (last 30 days)
Show older comments
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?
Answers (1)
Stephen23
on 10 Oct 2014
Edited: Stephen23
on 10 Oct 2014
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 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!