tags:

views:

45

answers:

3

Given the following strings

7;#User One
7;#User Two;#9;#User Two
7;#User Two;#9;#User Two;#123;#User Three

I would like to build a regular expression that "breaks" these apart so that each string returns the following matches:

["7;#User One"]
["7;#User Two", "9;#User Two"]
["7;#User Two", "9;#User Two", "123;#User Three"]

I tried a few methods but can't seem to get it to work properly. Can anyone help?

+3  A: 

This one should do the trick

#?([0-9]+;#[a-zA-Z\s]+)
Chris Meek
The first group will contain the match you want
Chris Meek
First to answer - but all do the trick thanks.
kouPhax
+1  A: 

Here you go:

#?\d*;#User [a-zA-Z]*[|#]?
Paul Sasik
+1  A: 

The following will give you matches on the group UserName

#*(?<UserName>\d+;#[^;]+)

It would be simply is you prepended the string with a hash and appended a semi-colon...

Justin Wignall
Yeah, Chris's start is better. would still go for ^; instead of a-z so you can have any character in user except semi-colon.#?(\d+;#[^;]+)
Justin Wignall