1

I'm wrote a pattern

string pattern2 = @"(?[<ports>\w+,*]*)";

This pattern should help parse string of next format

[port1, port2_4][portN_][port,port2,port5,p0_p1]

After parsing I'm want to have array of strings:

1. port1, port2_4
2. portN_
3. port, port2,port5,p0_p1
8
  • What question mark in the beginning means? Commented Nov 27, 2013 at 23:08
  • As I'm understood this means start of the group Commented Nov 27, 2013 at 23:09
  • Could you wrote some more constructive comments, please? If you know how to change my pattern for my task, please write your kind of pattern Commented Nov 27, 2013 at 23:13
  • 1
    I suggest getting Expresso ( its free ) and it will help with creating C# regex Commented Nov 27, 2013 at 23:13
  • 3
    @pipsik: I'm used to thinking people come here to learn. But sometimes (like now) they for a free fish. Do you realize that you'll learn nothing if you just copy-paste from other people answers? Commented Nov 27, 2013 at 23:14

1 Answer 1

2

this will work...

(?<=\[)(?<ports>.*?)(?=\])
  • (?<=\[) prefix of [ but dont include in match
  • (?<ports>.*?) named capture "ports" match anything non greedy ( as little as possible)
  • (?=\]) suffix of ] but don't include it in the match

code :-

Regex regex = new Regex(@"(?<=\[)(?<ports>.*?)(?=\])");
var m = regex.Matches("[port1, port2_4][portN_][port,port2,port5,p0_p1]");
foreach (var p in m)
{
    Console.WriteLine(p);
}

output :

port1, port2_4
portN_
port,port2,port5,p0_p1
Sign up to request clarification or add additional context in comments.

3 Comments

portNames always inside a brackets "[]". Port names may be separated by ",". Please, check my example at my main post
No. The content of one couple of [] should be one element of string array
Deleted that comment whilst you were posting, I too was tricked by the formatting of the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.