Function to extract certain elements from a character array
4 ビュー (過去 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 で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!