Python正则表达式GUI

 话不多说,开始今天的内容

import re
import webbrowser
from easygui import *
from tkinter.messagebox import *
import tkinter
import pyperclip


root = tkinter.Tk()
root.withdraw()

match_ans = ''
match_ans2 = ''
match_ques = ['', '', '', '', '']
lmatch_ans = 0
replace_ans = ''
replace_ques = ['', '', '', '']
while True:
    ans = buttonbox('请选择功能', '选择', ['教程', '使用必看', '匹配表达式+替换', '替换文本'])

    if ans == '教程':
        webbrowser.open('https://www.runoob.com/regexp/regexp-tutorial.html')
    elif ans == '使用必看':
        msgbox('每次的显示的结果会复制到剪贴板上\n第一行为搜索结果\n第二行为替换结果')
    elif ans == '匹配表达式+替换':
        while True:
            match_ques = multenterbox('请输入', '匹配表达式+替换', ['请输入表达式', '请输入文本', '请输入要替换的内容', '搜索结果', '替换结果'], [match_ques[0], match_ques[1], match_ques[2], f'共匹配{lmatch_ans}项{match_ans}', match_ans2])
            if match_ques == None:
                match_ans = ''
                match_ans2 = ''
                match_ques = ['', '', '']
                lmatch_ans = 0
                break
            else:
                try:
                    match_ans = re.findall(match_ques[0], match_ques[1])
                    if match_ans == ['']:
                        lmatch_ans = 0
                    else:
                        lmatch_ans = len(match_ans)

                    match_ans2 = re.sub(match_ques[0], match_ques[2], match_ques[1])

                    pyperclip.copy(f'共匹配{lmatch_ans}项{match_ans}\n{match_ans2}')
                except:
                    showerror('错误', '正则表达式输入错误')
    elif ans == '替换文本':
        while True:
            replace_ques = multenterbox('请输入', '替换文本', ['请输入文本', '请输入要替换的文本', '请输入替换内容', '结果'], [replace_ques[0], replace_ques[1], replace_ques[2], replace_ans])
            if replace_ques == None:
                replace_ans = ''
                replace_ques = ['', '', '', '']
                break
            else:
                try:
                    replace_ans = str(replace_ques[0]).replace(replace_ques[1], replace_ques[2])
                    pyperclip.copy(replace_ans)
                except:
                    showerror('错误', '无法替换')
    else:
        break

如果喜欢作者可以打赏一下

点击这里即可打赏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值