Alireza378n A
Alireza378n A

Reputation: 135

C# RegExp for search inner pattern

I have a string like this:

[[[a]]][[[b]]][[[c]]]

I want to extract these:

a
b
c

So I wrote the following pattern:

@"\[\[\[(.+?)\]\]\]"

using the following code

string input = "[[[a]]][[[b]]][[[c]]]";
Regex regexObj = new Regex(@"\[\[\[(.+?)\]\]\]");
foreach (Match er in regexObj.Matches(input)) 
{ 
    MessageBox.Show(er.Value); 
} 

the result is:

[[[a]]]
[[[b]]]
[[[c]]]

What is the matter? Could you help me?

Upvotes: 0

Views: 72

Answers (3)

Andie2302
Andie2302

Reputation: 4897

Try this:

[^\p{Ps}\p{Pe}]

This uses the unicode opening and closing brackets.

Upvotes: 0

Mattias
Mattias

Reputation: 1110

Why not this pattern (it gives the correct output for your example)?

[a-z]

Upvotes: -1

Andrey Korneyev
Andrey Korneyev

Reputation: 26876

Instead of er.Value you need to use er.Groups[1].Value.

er.Value is the same as er.Groups[0].Value, and it contains a string that matches the entire regular expression pattern. Each subsequent element, from index one upward, represents a captured group.

See MSDN for reference.

Upvotes: 3

Related Questions