Gyuzal
Gyuzal

Reputation: 1591

Regex to get a word between slashes

I'd appreciate if someone could help me with regex. I'm not sure how to figure one regex for several cases.

I have a lines with text like:

/REPORTMONTH/April
/CONTRACT/2/156/0001/285
//DATE/24032013

I need to get the first word after slash / or two slashes // at the beginning:

i.e. REPORTMONTH, CONTRACT, DATE. This should be the first item.

The second item is to get everything after a word with /:

i.e. April, 2/156/0001/285, 24032013

As you see the word 2/156/0001/285 has slashes as well. So, it should be taken into consideration.

Thanks a lot!

Upvotes: 1

Views: 1150

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Here is another example:

var pattern = new Regex(@"^//?(?<first>[^/]+)/(?<rest>.*)$",
    RegexOptions.Multiline);

//`${first}` will contain the first element, and
//`${rest}` will contain the remaining part of the line.

Upvotes: 1

anubhava
anubhava

Reputation: 784898

You can uss this regex:

^/+([^/]+)/(.*)$

And grab the text in captured groups #1 and #2.

Upvotes: 2

Related Questions