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!