You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何从指定字符串中提取DisplayName属性对应的取值

实现方案

你可以通过正则匹配直接定位目标字段前后的标记,提取中间内容,以下是不同常用语言的实现方式:

方法1:Python 实现

直接用re.search匹配目标片段即可:

import re

# 你的原始字符串
raw_str = '''<UserInputs><UserInput Question="Groupname" Answer="&lt;Values Count=&quot;1&quot;&gt;&lt;Value DisplayName=&quot;AllHummanresources&quot; Id=&quot;af05c5d3-2312-c897-8439-08979d4d0a49&quot; /&gt;&lt;/Values&gt;" Type="System.SupportingItem.PortalControl.InstancePicker" /><UserInput Question="Ausgabe" Answer="Namen" Type="richtext" /></UserInputs>'''

# 正则规则:匹配DisplayName=&quot;和下一个&quot;之间的内容
match_res = re.search(r'DisplayName=&quot;(.*?)&quot;', raw_str)
if match_res:
    target = match_res.group(1)
    print(target) # 输出:AllHummanresources
  • 正则里的.*?是非贪婪匹配,保证只会取到紧随其后的第一个&quot;之前的内容,不会多截取
  • 如果字符串里存在多个DisplayName字段,可以用re.findall替换re.search,会返回所有匹配到的结果列表

方法2:JavaScript 实现

const rawStr = `<UserInputs><UserInput Question="Groupname" Answer="&lt;Values Count=&quot;1&quot;&gt;&lt;Value DisplayName=&quot;AllHummanresources&quot; Id=&quot;af05c5d3-2312-c897-8439-08979d4d0a49&quot; /&gt;&lt;/Values&gt;" Type="System.SupportingItem.PortalControl.InstancePicker" /><UserInput Question="Ausgabe" Answer="Namen" Type="richtext" /></UserInputs>`

const reg = /DisplayName=&quot;(.*?)&quot;/
const matchRes = rawStr.match(reg)
if (matchRes) {
    const target = matchRes[1]
    console.log(target) // 输出:AllHummanresources
}

可选方案:先解码再解析XML(适合需要提取多字段的场景)

如果你后续还要提取Id、Count等其他字段,可以先把HTML实体解码,再解析XML结构取值,稳定性更高,以Python为例:

import html
import xml.etree.ElementTree as ET

raw_str = '''你的原始字符串'''
# 先提取Groupname对应的Answer属性里的XML内容
answer_xml = html.unescape(ET.fromstring(raw_str).find("./UserInput[@Question='Groupname']").attrib['Answer'])
# 解析XML取DisplayName属性
root = ET.fromstring(answer_xml)
target = root.find("./Value").attrib['DisplayName']
print(target) # 输出:AllHummanresources

内容的提问来源于stack exchange,提问作者rolfderwolf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:54:03