简体   繁体   English

正则表达式-匹配捕获组中的第一个和最后一个字符

[英]Regex - Match first and last character within capturing group

I want to capture the first and last character within a capturing group. 我想捕获一个捕获组中的第一个和最后一个字符。

My current RegEx is - 我当前的RegEx是-

([\w\.]+)@([\w]+)\.com

For example, if there is an email address - 例如,如果有一个电子邮件地址-

xyz@test.com

This is the output - 这是输出-

Full match  0-12    `xyz@test.com`
Group 1.    0-3 `xyz`
Group 2.    4-8 `test`

The email address can have alphanumeric and period values. 电子邮件地址可以包含字母数字和句点值。

If I want to curtail the Group 1 such that it starts and ends with only alphanumeric values, how to do that? 如果我想减少组1使其仅以字母数字值开头和结尾,该如何做?

I want to modify this capturing group - 我要修改此捕获组-

 ([\w\.]+)

The required output is - 所需的输出是-

xyz.@test.com Invalid
.xyz@test.com Invalid
xy.z@test.com Valid

To tell engine match English alphanumeric characters at the start position and one before @ you need to do this: 要告诉引擎匹配的英文字母数字字符在开始位置和@之前,您需要执行以下操作:

^([a-zA-Z0-9][\.a-zA-Z0-9]*[a-zA-Z0-9])@([a-zA-Z0-9]+)\.com$

Note: \\w includes _ that you may not desire. 注意: \\w包含您可能不希望的_

But this doesn't allow usernames with one character long. 但这不允许使用一个字符长的用户名。 So you have to modify it a little: 因此,您必须对其进行一些修改:

^([a-zA-Z0-9]+(?:\.+[a-zA-Z0-9]+)*)@([a-zA-Z0-9]+)\.com$

Also this shouldn't be considered a good email validator. 同样,这不应该被认为是一个很好的电子邮件验证器。 But as it seems you narrow down matching to .com TLD so I assume this is a very specific requirement otherwise it limits domain name to alphanumerics and doesn't allow many more characters that would be valid in an email address according to RFC 822 . 但是,似乎您缩小了对.com TLD的匹配范围,因此我认为这是一个非常具体的要求,否则它将域名限制为字母数字,并且不允许再有许多根据RFC 822在电子邮件地址中有效的字符。 This would be enough for capturing an email address from user input: 这足以从用户输入中捕获电子邮件地址:

^[^\s@]+@[^\s@]+$

This works: 这有效:

^([0-9a-zA-Z][a-zA-Z0-9_\.]*)(?<!\.)@([a-zA-Z0-9_]+)\.com$

Demo 演示版

Basically, it tries to match alphanumeric characters at the start, then [a-zA-Z0-9_\\.] for 0 or more times. 基本上,它会尝试从头开始匹配字母数字字符,然后匹配[a-zA-Z0-9_\\.] 0次或更多次。 Before it reaches @ , it will look behind to check if there is a dot (if it is not an alphanumeric, it's gotta be a dot). 在到达@之前,它将向后检查是否有一个点(如果不是字母数字,则必须是一个点)。

试试这个正则表达式-(^ [\\ w] [\\ w \\。\\ w] + [\\ w])@([[ww ++)\\。com

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

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