1. 利用正则表达式模块,获取输入的邮箱地址中的用户名。
输入一串邮箱地址字符串,输出其中的用户名
样例
输入:user@163.com
输出:user
麻烦用正则表达式写下代码谢谢哈
import re
email='user@163.com'
pattern='[a-zA-Z]+(?=\@)'
res=re.search(pattern,email).group()
print(res)
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