Regular expression to capture multiple groups separated by a leading delimiter

I have a line like this:

|T1| This is some text for the first tag |T2| this is some text for the second tag

      

I need to parse the tags and the text that is associated with each one. Tags are not known in advance, but they are limited \|\w+\|

.

I know there is something I can do here regarding groups etc., but after the mess in powershell, the best I can think of is to first isolate each pairing \|\w+\|.*

using ExplicitCapture parameter and then parse out there tag and text.

But it does double work and is completely not a super-cool haxor. What is this regular expression?

Edit: Actually I understand that this is late and I misunderstood my results. The above doesn't work, so now I don't even have a bad solution.

+3


source to share


1 answer


\|(?<tag>\w+)\|(?<text>[^|]*)

      

Matches |T1| This is some text for the first tag |T2| this is some text for the second tag

in

 |T1| This is some text for the first tag 
 |T2| this is some text for the second tag

      



EDIT : Use regex groups to get parts of the match;

var tagName = match.Groups["tag"].Value;
var text = match.Groups["text"].Value;

      

Switch to named groups instead of numbered ones

+4


source







All Articles