0

我在编写一个正则表达式时需要帮助,我想在其中删除电子邮件地址开头和结尾中不需要的字符。例如:

z>user1@hotmail.com<kt
z>user2@hotmail.pk<kt
z>puser3@yahoo.com<kt
z>npuser4@yaoo.uk<kt

应用正则表达式后,我的电子邮件应如下所示:

user1@hotmail.com
user2@hotmail.pk
puser3@yahoo.com
npuser4@yaoo.uk

如果电子邮件地址已经正确,则不应应用正则表达式。

4

4 回答 4

3

您可以尝试删除匹配项

^[^>]*>|<[^>]*$

演示


正则表达式可视化

调试演示

于 2013-11-06T16:48:13.073 回答
0

Find ^[^>]*>([^<]*)<*.*$ and replace it with \1

Here's an example on regex101

于 2013-11-06T16:57:01.160 回答
0

Try using a capturing group on anything between the characters you don't want. For example,

/>([\w|\d]+@[\w\d]+.\w+)</

Basically, any part that the regexp inside () matches is saved in a capturing group. This one matches anything that's inside >here< that starts with a bunch of characters or digits, has an @, has one or more word or digit characters, then a period, then some word characters. Should match any valid email address.

If you need characters besides >< to be matched, make a character class. That's what those square bracketed bits are. If you replace > with [.,></?;:'"] it'll match any of those characters.

Demo (Look at the match groups)

于 2013-11-06T16:57:09.503 回答
0

我认为您可能会稍微遗漏正则表达式的要点。正则表达式定义字符串的“形状”并返回字符串是否符合该形状。电子邮件地址的简单表达式可能类似于:[az][AZ][0-9]*.?[az][AZ][0-9]+@[az][AZ][0-9] *.[az]+

但是为电子邮件地址编写一个包罗万象的正则表达式并不简单。确实,您需要做的是正确检查它:

  1. 确保只有一个“@”符号。

  2. 检查 at 符号之前的部分是否符合该部分的正则表达式:

    • 人物
    • 数字
    • 扩展字符:.-'_(该列表可能不完整)
  3. 检查 @-sign 之后的部分是否符合域名的 reg-ex:
    • 人物
    • 数字
    • 扩展字符: . -
    • 必须以字符或数字开头,并且必须以正确的域名结尾。
于 2013-11-06T16:58:21.417 回答