简体   繁体   English

正则表达式 (Javascript) 用于解析带有 firstname.lastname.randomnumber@email.com 的电子邮件

[英]Regex (Javascript) for parsing email with firstname.lastname.randomnumber@email.com

I've scoured all the other threads about this but there's a unique aspect to my parsing format that I can't get past.我已经搜索了所有关于此的其他线程,但是我的解析格式有一个我无法克服的独特方面。 I'm trying to parse an email address with a standard format as follows - firstname.lastname.#####@email.com ie 'john.smith.12345@email.com'我正在尝试使用标准格式解析电子邮件地址,如下所示 - firstname.lastname.#####@email.com ie 'john.smith.12345@email.com'

I want to return any alpha characters before the numeric sequence - 'john smith' (or all names before the .12345 in case people have multiple names).我想在数字序列之前返回任何字母字符 - 'john smith'(或者 .12345 之前的所有名字,以防人们有多个名字)。

Right now, I've worked out the regex to (^[A-Za-z]+\\.+[A-Za-z]+) but that returns 'john.smith' which isn't the end of the world since I can split it.现在,我已经计算出(^[A-Za-z]+\\.+[A-Za-z]+)的正则表达式,但这会返回 'john.smith' 这不是世界末日因为我可以拆分它。

Any help would be much appreciated.任何帮助将非常感激。

Click on the code blocks below to try them out.单击下面的代码块进行试用。

If you only have two or three dot-separated names before the numbers, you can hardcode the group alternatives:如果数字前只有两个或三个点分隔的名称,则可以对组替代项进行硬编码:

(?:([A-Za-z]+)\\.([A-Za-z]+)|([A-Za-z]+)\\.([A-Za-z]+)\\.([A-Za-z]+))\\.\\d+@[^\\s@]+

Here we use a non-capturing group to enclose the alternatives.在这里,我们使用一个非捕获组来包含备选方案。

For one, two, or three dot-separated names, the pattern is similar (albeit a bit messier).对于一个、两个或三个点分隔的名称,模式是相似的(尽管有点混乱)。


Another approach would be to wrap some of the preceding dot + capturing group sections with an optional non-capturing group.另一种方法是用可选的非捕获组包装前面的一些点 + 捕获组部分。

([A-Za-z]+)\\.([A-Za-z]+)(?:\\.([A-Za-z]+))?\\.\\d+@[^\\s@]+

This enables a more concise expression of 1-3 dot-separated names as:这使得 1-3 个点分隔名称的更简洁表达为:

([A-Za-z]+)(?:\\.([A-Za-z]+))?(?:\\.([A-Za-z]+))?\\.\\d+@[^\\s@]+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM