1

I tried \w+\:(\w+\-?\.?(\d+)?) but that is not correct

I have following text

<staticText:HelloWorld>_<xmlNode:Node.03>_<date:yyy-MM-dd>_<time:HH-mm-ss-fff>

The end result I want is something like the following

["staticText:HelloWorld", "xmlNode:Node.03","date:yyy-MM-dd","time:HH-mm-ss-fff"]
Emma
  • 27,428
  • 11
  • 44
  • 69
Kicker
  • 33
  • 1
  • 7
  • As an aside.. https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Caius Jard May 30 '19 at 17:16
  • 1
    Can you take the simplistic approach of `x.Replace(">_<", "\",\"")` plus trimming the start/end? – Caius Jard May 30 '19 at 17:17
  • Try using: <(.*?)> I don't understand why you need it in this specific format: ["staticText:HelloWorld", "xmlNode:Node.03","date:yyy-MM-dd","time:HH-mm-ss-ff"] – Jake Steffen May 30 '19 at 17:21

4 Answers4

2

You could use the following regex.

<(.*?)>

Then have a look at how groups work to retrieve the result.

Regex rx = new Regex("<(.*?)>");
string text = "<staticText:HelloWorld>_<xmlNode:Node.03>_<date:yyy-MM-dd>_<time:HH-mm-ss-fff>";

MatchCollection matches = rx.Matches(text);
Console.WriteLine(matches.Count);

foreach(Match match in matches){
    var groups = match.Groups;
    Console.WriteLine(groups[1]);
}
Oflocet
  • 566
  • 3
  • 9
  • 19
2

This line should be able to match the content:

<(.*?)>

It will catch the arrows at the end which you don't seem to want, but you could remove them after words without regex.

You should consider a website like https://regexr.com - it helps exponentially in writing regex by allowing you to paste your cases and see how it works with them.

Zachary Brooks
  • 303
  • 2
  • 9
2

Matches any string within the <>. Hope this helps.

<(.*?)>

juju_banton
  • 56
  • 2
  • 4
1

Your pattern does not match the 3rd and the 4th part of the example data because in this part \w+\-?\.?(\d+)? the dash and the digits match only once and are not repeated.

For your example data, you might use a character class [\w.-]+to match the part after the colon to make the match a bit more broad:

<(\w+\:[\w.-]+)>

Regex demo | C# demo

Or to make it more specific, specify a pattern for either the Node.03 part and for the year month date hour etc parts using a repeated pattern.

<(\w+\:\w+(?:\.\d+|\d+(?:-\d+)+)?)>

Explanation

  • < Match <
  • ( Capturing group
    • \w+\:\w+ Match 1+ word chars, : and 1+ word chars
    • (?: Non capturing group
      • \.\d+ Match . and 1+ digits
      • | Or
      • \d+(?:-\d+)+ Match 1+ digits and repeat 1+ times matching - and 1+ digits
    • )? Close non capturing group and make it optional
  • ) Close capturing group
  • >

Regex demo | C# Demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70