-1

I have a below xml

<ns1:Header>
<ns4:Name>Mohan</ns4:Name>
<ns4:Age>10</ns4:Age>
<ns4:Dept>CSE</ns4:Dept>
</ns1:Header>

I need to write a regex to find the Name from Mohan tag. Here the namespace(ns4) may change dynamically .Please help to write a generic regex that should work in all the namespace

Mohan
  • 3,893
  • 9
  • 33
  • 42

2 Answers2

2

Try this:

<ns\d+:Name>(.+)<

But do you really have to use RegEx? There are better ways, like System.Xml.XmlDocument class in .NET

AdamL
  • 12,421
  • 5
  • 50
  • 74
1

binogure's solution seems right, I'd only make the namespace more general:

/<(\w+:Name)>(\w+)<\/\1>/

Or, if the namespace if optional:

/<((?:\w+)?Name)>(\w+)(\/\1>/

The name would be in the second capturing group.

Loamhoof
  • 8,293
  • 27
  • 30