Function to extract certain elements from a character array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
Antworten (1)
Stephen23
am 10 Okt. 2014
Bearbeitet: Stephen23
am 10 Okt. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!