C# 正则表达式初级篇

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace testreg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       

        void Regex_string(string Str_text,string Str_y,string Str_n,string Str_reg)
        {
            if (Regex.IsMatch(Str_text, Str_reg))
            {
                MessageBox.Show(Str_y);
            }
            else
            {
                MessageBox.Show(Str_n);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Regex_string(textBox1.Text.Trim(), "有汉字", "没汉字", @"[^/x00-/xff]");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Regex_string(textBox2.Text.Trim(), "是", "否", @"[/u4e00-/u9fa5]");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Regex_string(textBox3.Text.Trim(), "不是数字", "是数字", @"[^0-9]");
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
          
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            //用正则替换掉非数字的字符
            Regex regex =new Regex(@"[^0-9]");
            string Str_tb4 = textBox4.Text.Trim();
            Str_tb4 = regex.Replace(Str_tb4, "");
            textBox4.Text = Str_tb4;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            //用正则替换掉非汉字的字符
            Regex regex = new Regex(@"[/x00-/xff]");
            string Str_tb4 = textBox5.Text.Trim();
            Str_tb4 = regex.Replace(Str_tb4, "");
            textBox5.Text = Str_tb4;
        }
    }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值