您如何使用现有正则义务的正则表达式检索n个字符?
我需要从电子邮件地址检索电子邮件ID。 (IE
我使用的正则态度是(。*)@。*
。
现在,我需要用n个字符截断字符串。 (即n = 7 => this-is n = 30 => this-is-the-best.Email)
如何将其添加到现有的正则拨号中? 还有其他建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那:
([^@] {1,7})。+
?变成:
What about:
([^@]{1,7}).+
?Becomes:
我认为这就是您要寻找的:
第一个捕获组包含完整的ID,第二组最多包含7个字符。
I think that this is what you are looking for:
The first capturing group contains the full id and the second group contains up to 7 chars.
在您的模式
(。*) @。*
中,您不需要尾随。*
是可选的,并且点可以匹配任何字符,包括空格和 @它本身可以匹配更多的,只有电子邮件地址。有趣的是,在实际匹配 @之前,不包括 @ char的非空格字符,在这种情况下,您可以使用匹配7个非Whitespace Chars的捕获组。
模式匹配:
([^\ s@] {7})
捕获组1 ,匹配7 non Whitespace chars不包括@[^\ s@]*
(可选)匹配任何非whitespace char不包括 @@
字面上匹配In your pattern
(.*)@.*
you don't need the trailing.*
as it is optional, and the dot can match any character including spaces and the @ itself which can match much more that just an email like address.The thing of interest is the non whitespace chars excluding an @ char before actually matching the @, and in that case you can use a capture group matching 7 non whitespace chars.
The pattern matches:
([^\s@]{7})
Capture group 1, match 7 non whitespace chars excluding @[^\s@]*
Optionally match any non whitespace char excluding @@
Match literallyRegex demo