Click here to Skip to main content
15,885,767 members

Is there a way to simply a regular expression?

Primo Chalice asked:

Open original thread
I have an XML as follows:

XML
<break name="article_1-1">
<h1>
  <page num="1" />Some heading</h1>
<bl>
  Human name Contributing Writer</bl>
<h3>
  OPINION
</h3>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
<bq>
  Some value
</bq>
<p>
  Fourth Paragraph with italic values
</p>
<fig>
  <img src="images/img_1-1.jpg" width="1553" height="1050" alt="" />
  <fc>
	Image caption
  </fc>
  <cr>PHOTOGRAPHS BY SOME HUMAN</cr>
</fig>
<h3>
  CITY, STATE
</h3>
</break>


I want to make it like:

XML
<break name="article_1-1">
<h1><page num="1" />Some heading</h1>
<bl>Human name Contributing Writer</bl>
<h3>OPINION</h3>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
<bq>Some value</bq>
<p>Fourth Paragraph with italic values</p>
<fig><img src="images/img_1-1.jpg" width="1553" height="1050" alt="" /><fc>Image caption</fc><cr>PHOTOGRAPHS BY SOME HUMAN</cr></fig>
<h3>CITY, STATE</h3>
</break>


I am removing the indentation at a later stage but my main focus is on bringing the opening and closing XML tags in the same line.

I want a regex for this. I have tried something but I think there is a better way.

Please help.

Regards

What I have tried:

C#
string pattern = @"(?:(?:(<\w.>)|(<\w>)|(<\w..>|(<p>)|(\/>)))(\s+)|((<\/(?!(title)|(head)|(break)|(body))\w+>)(\s+)(<\/(?!(title)|(head)|(break)|(body))\w+>))|((<\/fc>)(\s+)(<cr>)))";

string substitution2 = @"$1$2$3$8$14$20$22";
Tags: C#, .NET, Visual Studio

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900