-1

I need help writing a regular expression that moves content from the title tag into the content tag.

This:

<xml>
 <item>
  <title>Title 1</title>
  <content>Text 1</content>
 </item>
 <item>
  <title>Title 2</title>
  <content>Text 2</content>
 </item>
</xml>

To this:

<xml>
 <item>
  <title>Title 1</title>
  <content>Title 1 Text 1</content>
 </item>
 <item>
  <title>Title 2</title>
  <content>Title 2 Text 2</content>
 </item>
</xml>

Edit: I made a new topic with a better explaination of my question: Regular expression - moving content between XML tags

SORRY!

Community
  • 1
  • 1
Dylan
  • 158
  • 1
  • 2
  • 10
  • Does it need to be a regex-based solution? – Álvaro González Dec 19 '14 at 10:16
  • [Do not parse XML with regular expressions.](http://stackoverflow.com/a/1732454/695343) – Jens Erat Dec 19 '14 at 10:28
  • Do not duplicate your own questions. Instead edit the original one. - I now closed the original question as I already closed the duplicate one. You should make more clear in your question what you tried already so it's more specific with which part you actually have the question. Showing your own code does help in making this more clear in your question. – hakre Dec 19 '14 at 14:27

3 Answers3

0
(<title>((?:(?!<\/title>).)*)<\/title>\s*<content>)((?:(?!<\/content>).)*)

Try this.Replace by

$1$2 $3

See demo.

https://regex101.com/r/vN3sH3/22

$re = "/(<title>((?:(?!<\\/title>).)*)<\\/title>\\s*<content>)((?:(?!<\\/content>).)*)/";
$str = "<xml>\n <item>\n <title>Title 1</title>\n <content>Text 1</content>\n </item>\n <item>\n <title>Title 2</title>\n <content>Text 2</content>\n </item>\n</xml>";
$subst = "$1$2 $3";

$result = preg_replace($re, $subst, $str);
vks
  • 67,027
  • 10
  • 91
  • 124
0

First of all, using regex for parsing domnodes is bad, there are dom-parsers that help better. Expressions for matching title tag content:

RegEx with minimal flag (non-greedy, non matching newline):

\<title\>(.*)\</title\>

RegEx otherwise:

\<title\>([^\</title\>]*)\</title\> 
kero
  • 10,647
  • 5
  • 41
  • 51
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
0

Use DOTALL modifier s to make dot in your regex to match newline characters also.

Regex:

~(<title>([^<>]*)<\/title>.*?<content>)~s

Replacement string:

\1\2 

DEMO

$re = "/(<title>([^<>]*)<\\/title>.*?<content>)/s";
$str = "<xml>\n <item>\n <title>Title 1</title>\n <content>Text 1</content>\n </item>\n <item>\n <title>Title 2</title>\n <content>Text 2</content>\n </item>\n</xml>";
$subst = "\1\2 ";
$result = preg_replace($re, $subst, $str);
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274