How to extract before "@" on email adress

Viewed 43

I'd like to extraxt string "test", which is the letters before "@",by using RegExp like following, but actually could get "test@" including "@". How can I get only "test" without "@" ?

final getPass = "test@example.com";
final regEx = RegExp(r'(.+?)@');
print(regEx.firstMatch(getPass)?.group(0));
2 Answers

If using a Regexp is not mandatory for you, you could use String methods :

final getPass = "test@example.com";
print(getPass.split("@").first);

Use .group(1)

final getPass = "test@example.com";
final regEx = RegExp(r'(.+?)@');
print(regEx.firstMatch(getPass)?.group(1));
Related