Python使用RegEx操作字符串

2024-05-29 03:49:43 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是Active Directory组的可分辨名称。我想将最左边的名称从DN的其余部分中分离出来,如下所示:

CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo=CTX_APP_Bytemobile_UPM

CN=OSGRP_IP_传输,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo=OSGRP_IP_传输

CN=远程桌面用户,CN=内置,DC=ssa,DC=oam,DC=uk,DC=tmo=远程桌面用户

到目前为止,我的正则表达式只匹配''uu'字符串。我的正则表达式是:

(?<=CN=)\w*

我也在尝试如何在Python中使用're'模块。目前我的命令是:

^{pr2}$

我想获得一个新的字符串匹配。在

提前谢谢。在


Tags: 名称appoudccnssactxuk
3条回答

似乎您可以不使用regex而只使用str.split。例如:

s = 'CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo'

result = s.split(',')[0].split('=')[1]
print(result)
# CTX_APP_Bytemobile_UPM

如果你只想知道等号后面是什么,你可以在逗号上拆分,把一个变量设置到等号加一的位置,然后一直读到最后。在

presplit = "CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo"
#Make a list of strings split on the comma
lst = presplit.split(",")
#Iterate through the list
for i in lst:
    #Set the starting position to where the equal sign is plus one
    strt = re.search("=", i).start()+1
    #Get the string from the character after the equal sign to the end of the string
    print(i[strt:])

尝试:x = re.search("(?<=CN=)[\w\s]*", presplit)

相关问题 更多 >

    热门问题