English 中文(简体)
我能用什么办法从一个没有改动的文本中抽取一套XML案文?
原标题:What regex could I use to extract a body of XML text from a body of unformatted text?
  • 时间:2010-09-16 17:44:33
  •  标签:
  • .net
  • regex

我要说的是,案文如下:

Call me Ishmael. Some years ago- never mind how long precisely- having little 
or no money in my purse, and nothing particular to interest me on shore, I 
thought I would sail about a little and see the watery part of the world. It is  
<?xml version="1.0" encoding="utf-8"?>
<RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <ChildElement />
   <ChildElement />
</RootElement>
a way I have of driving off the spleen and regulating the circulation. Whenever  
I find myself growing grim about the mouth; whenever it is a damp, drizzly 
November in my soul; 

我能用什么reg?

<>m>NOTE:我可以假设:<RootElement></RootElement>将始终有相同的名称。

最佳回答

如果你知道,根本要素永远是<代码><RootElement ......>,并且永远不会有一条封顶的<代码><RootElement>tag,你可以这样做:

<?xml .+?</RootElement>

这一条码将贴上<条码>和>之间的所有文本;?xml和</RootElement>

问题回答

我的理解是,根本内容并不总是被称作RootElement,因此你可以使用。

<?xml[^>]+>s*<s*(w+).*?<s*/s*1>

采用<代码>RegexOptions。 单信用证/代码。 这将在打开“标签”后排出首个标签,并在对等标之前抓住一切。

C#:

resultString = Regex.Match(subjectString, @"<?xml[^>]+>s*<s*(w+).*?<s*/s*1>", RegexOptions.Singleline).Value;




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签