如何解析字符串并从括号语法之间提取文本(例如< ID> 12321< / ID>我使用以下字符串格式:
<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>
理想情况下,我想将所有数字作为文本读入字符串数组。这些叮咬可能有多达一百个ID值。
是的,我从XML开始。我正在使用一种格式给我标记:
[xml]$XMLResponse = $webclient.DownloadString("http://url.com/file.xml")
Write-Output $XMLResponse."string".ChildNodes
输出结果为:
Name : #text
LocalName : #text
NodeType : Text
ParentNode : string
Value : <ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>
InnerText : <ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>
Data : <ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>
Length : 2464
PreviousSibling :
NextSibling :
ChildNodes : {}
Attributes :
OwnerDocument : #document
FirstChild :
LastChild :
HasChildNodes : False
NamespaceURI :
Prefix :
IsReadOnly : False
OuterXml : <ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>
InnerXml :
SchemaInfo : System.Xml.Schema.XmlSchemaInfo
BaseURI :
答案 0 :(得分:1)
所有ID值是否都是六位数?您可以使用正则表达式。
$Text = '<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . <TITLE><ID>192723</ID><TITLE>Loreum Ipsum<TITLE><ID>298343</ID><TITLE>Thanks for help<TITLE><ID>192723</ID><TITLE>Strings are hard<TITLE>';
$MatchList = ([Regex]'(?<=<ID>)(\d{6})(?=</ID>)').Matches($Text);
$MatchList.Value;
结果:
298343
192723
298343
192723
注意:由于您的源文本,存在重复值。