VBA正则切取字符串中的英文和数字和下划线

博客展示了使用Python的re模块进行正则表达式匹配的代码。通过定义正则表达式模式,使用findall方法在示例文本中查找所有匹配项,并打印出匹配结果,体现了正则表达式在字符串处理中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function RegTest(ByVal sText As String) As String

    '定义正则表达式对象

    Dim oRegExp As Object

    '定义匹配字符串集合对象

    Dim oMatches As Object

    '创建正则表达式
    Dim a As String
    


    '定义要执行正则查找的文本变量

    Set oRegExp = CreateObject("vbscript.regexp")

    With oRegExp

        '设置是否匹配所有的符合项,True表示匹配所有, False表示仅匹配第一个符合项

        .Global = True

        '设置是否区分大小写,True表示不区分大小写, False表示区分大小写

        .IgnoreCase = True

        '设置要查找的字符模式

        '.Pattern = "[^x00-xff%&',;=?$x22]"
        .Pattern = "[A-Za-z0-9_]+"

        '判断是否可以找到匹配的字符,若可以则返回True

        MsgBox .Test(sText)

        '对字符串执行正则查找,返回所有的查找值的集合,若未找到,则为空

        Set oMatches = .Execute(sText)

        '把字符串中用正则找到的所有匹配字符替换为其它字符
        MsgBox (oMatches(0))
        
        a = oMatches(0)
        
        RegTest = a
        
        MsgBox .Replace(sText, "")

    End With

    Set oRegExp = Nothing

    Set oMatches = Nothing

End Function

Sub a()

    Dim Text As String
    
    Text = "这个是pooST11AD:USE=湖综后或者pooDACAO=NOT_USEDH笔记本大家"

    ak = RegTest(Text)
    
    MsgBox (ak)
    
End Sub
import re
import ast

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    d = {}
    d["a"] = "use"
    d["b"] = "use"
    print(d)
    check = 'a=="use" and b!="use"'
    print(eval(check, {}, d))



    checksw = "(defined(a) and b=='use')or(!defined(a) and b=='use')"

    pattern = r'!defined\(([^)]+)\)'
    matches = re.findall(pattern, checksw)
    print(matches)
    for m in matches:
        checksw = checksw.replace("!defined(" + str(m) + ")", str(m)+"!='NULL'")
    print(checksw)

    pattern2 = r'defined\(([^)]+)\)'
    matches = re.findall(pattern2, checksw)
    print(matches)
    for m in matches:
        checksw = checksw.replace("defined(" + str(m) + ")", str(m)+"=='NULL'")
    print(checksw)

    print(eval(checksw, {}, d))

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

import re

 

# 示例文本

text = "Here is a defineVariable_1 and another defineVariable_2."

 

# 正则表达式模式

pattern = r'define([A-Za-z0-9_]+)'

 

# 使用findall方法查找所有匹配项

matches = re.findall(pattern, text)

 

# 打印匹配结果

for match in matches:

    print(match)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值