用正则表达式提取邮箱用户名
半年前申请了一个空间,一直没有发表过文章,今天心情舒畅,发表一篇没啥技术含量的文章,希望对得起自己的空间,同时也是对自己的鼓励哈!(大家就别见笑了 呵呵)
实际上这是一次作业题,是关于用正则表达式提取邮箱用户名的。
效果如下:
输入完整的邮箱

提取结果

代码
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Text.RegularExpressions;
9
10 namespace Emai_name
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 this.Text = "Email_Name";
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 if (!(Regex.Match(textBox1.Text,@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").Success))
23 {
24 MessageBox.Show("输入邮箱格式不正确!");
25 }
26 else
27 MessageBox.Show("恭喜您,输入合法!");
28 //MatchCollection s = Regex.Matches(textBox1.Text, @"\b(?<=@).*");//输出@后面的字符串
29 MatchCollection s = Regex.Matches(textBox1.Text, @"\w+([-+.]\w+)*(?=@)");//输出@前面的字符串(用户名)
30 foreach(Match m in s)
31 {
32 textBox2.Text += m.Value + "\n";//提取结果
33 }
34 }
35 }
36 }
37


浙公网安备 33010602011771号