2

I have the following line of XML:

<indexentry><secondaryie>definition, 3/2/4</secondaryie></indexentry>

And I need a regex that matches the above and converts it as below:

ABC3(the first number)/P-2(second number)-4(third number)

How can I do this?

hopper
  • 13,060
  • 7
  • 49
  • 53
user2423959
  • 836
  • 1
  • 13
  • 27
  • 1
    Regex (at least alone) are not a great tool for this. Use an XML parser. – Konrad Rudolph Oct 22 '13 at 12:50
  • possible duplicate of [Can you provide some examples of why it is hard to parse XML and HTML with a regex?](http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg) – Ingo Oct 22 '13 at 12:52
  • 1
    Please don't [parse XML the Cthulhu way](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html). – Mauren Oct 22 '13 at 12:54
  • Hi @Mauren. Thanks for your comments, here actually i need an xsl transformation where in i use this regex. – user2423959 Oct 22 '13 at 13:04
  • 1
    Then anubhava's answer is probably a good way to solve your problem. – Mauren Oct 22 '13 at 13:07
  • Yes this has solved my problem, thanks guys for your support – user2423959 Oct 22 '13 at 13:27

1 Answers1

1

Use this regex:

([0-9]+)/([0-9]+)/([0-9]+)

And from captured groups #1, #2, #3 make your string:

ABC3\1/P-2\2-4\3
anubhava
  • 761,203
  • 64
  • 569
  • 643