-1

I'm trying to parse the following pattern

(\d+)\*\[(.+?)\]

Here's the string I'd like for it to parse correctly:

10*[1*[{0.1-0.9}(10)]]10*[1*[{0.2-0.3}(10)]]

there should be 2 match here with their group 2 being:

1*[{0.1-0.9}(10)] and
1*[{0.2-0.3}(10)]

unfortunately due to the .+? the matches are now:

1*[{0.1-0.9}(10)
1*[{0.2-0.3}(10)

both of them are missing the closing [, but the ? is needed otherwise I can't but them next to each other:

with the pattern being (\d+)\*\[(.+)\]: there is only 1 match with its group 2 being:

1*[{0.1-0.9}(10)]]10*[1*[{0.2-0.3}(10)]

Could anyone guide me on this issue? Maybe I need to do some kind of preparse? Pure regex would be much appreciated.

EDIT 1:

I would use this regular expression recursively thats why I need it to match both the inner and the outer pattern correctly, unfortunately both greedy and reluctant matching goes wrong with my pattern

EDIT 2:

My original question was vague, I'm sorry about that. Here's the catch, here's why I, myself couldn't do the correct regex string:

the pattern that matches 10*[1*[{0.1-0.9}(10)]]10*[1*[{0.2-0.3}(10)]] correctly into 1*[{0.1-0.9}(10)] and 1*[{0.2-0.3}(10)] should also be able to match the matches itself: e.g.: 1*[{0.1-0.9}(10)] into 1 and {0.1-0.9}(10)

maybe I shouldn't try with pure regex?

Qqbt
  • 802
  • 2
  • 8
  • 33
  • [`(\d+\*\[(.+?)\]\])`](https://regex101.com/r/hK0bL9/1) – Tushar Nov 21 '15 at 13:37
  • I was hastily with the thanks sure, but Edit 1 and Edit 2 are for clarification, they don't constitute a question on their own – Qqbt Nov 21 '15 at 14:18
  • Try [`\d+\*\[(?\d+)\*\[(?[^][]*)]]`](http://regexstorm.net/tester?p=%5cd%2b%5c*%5c%5b(%3f%3cdigit%3e%5cd%2b)%5c*%5c%5b(%3f%3cbr_expr%3e%5b%5e%5d%5b%5d*)%5d%5d&i=10*%5b1*%5b%7b0.1-0.9%7d(10)%5d%5d10*%5b1*%5b%7b0.2-0.3%7d(10)%5d%5d+). See *Table* tab showing captured texts, too. – Wiktor Stribiżew Nov 21 '15 at 14:38
  • @stribizhev yes, thank you, meanwhile I used a very similar expression, which indeed does what I want: (\d+)\*(\[(?>\[(?)|\](?<-DEPTH>)|[^\[\]]+)*\](?(DEPTH)(?!))) – Qqbt Nov 21 '15 at 14:39
  • @Qqbt: Looking at the question, it is not at all clear what you need *exactly*. Is it something like [`(?\d+)\*\[(?(?>[^][]+|\[(?)|](?<-n>))*)(?(n)(?!))]`](http://regexstorm.net/tester?p=(%3f%3cG1%3e%5cd%2b)%5c*%5c%5b(%3f%3cG2%3e(%3f%3e%5b%5e%5d%5b%5d%2b%7c%5c%5b(%3f%3cn%3e)%7c%5d(%3f%3c-n%3e))*)(%3f(n)(%3f!))%5d&i=10*%5b1*%5b%7b0.1-0.9%7d(10)%5d%5d10*%5b1*%5b%7b0.2-0.3%7d(10)%5d%5d+)? But `1` and `{0.1-0.9}(10)` are not in different groups. – Wiktor Stribiżew Nov 21 '15 at 14:53

1 Answers1

2

If you would like to match only the inner portion of the brackets, you could replace the reluctant .+? with greedy [^\[\]]+ that matches everything except square brackets:

var input = "10*[1*[{0.1-0.9}(10)]]10*[1*[{0.2-0.3}(10)]]";
var pattern = @"(\d+)\*\[([^\[\]]+)\]";
foreach (Match m in Regex.Matches(input, pattern)) {
    Console.WriteLine(m.Value);
}

This code prints

1*[{0.1-0.9}(10)]
1*[{0.2-0.3}(10)]

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • yes, this is true, unfortunaley this doesnt parse the first group (10*), so I can't use it for nested patterns – Qqbt Nov 21 '15 at 13:53
  • please take a look at EDIT 2 – Qqbt Nov 21 '15 at 14:06
  • @Qqbt If you need to match parentheses, .NET expressions provide [balancing groups](http://stackoverflow.com/a/19597456/335858) that let you do that. This is powerful enough for the expressions that you show, but once the complexity goes up you may be better served with a small parser. – Sergey Kalinichenko Nov 21 '15 at 14:11
  • thank you, i'll vote the question as too specific or not helpful, because I think this specific question won't help anyone anyway – Qqbt Nov 21 '15 at 14:13
  • @Qqbt I don't think the question is bad for the site, because it illustrates well the general idea of replacing reluctant groups `(.+?)` followed by a specific terminator (in your case, that's `]`) with greedy groups that match everything *except* the terminator. – Sergey Kalinichenko Nov 21 '15 at 14:21
  • true, but the real answer was your comment: balancing groups, I've already managed to parse correctly with the pattern: \[(?>\[(?)|\](?<-DEPTH>)|[^\[\]]+)*\](?(DEPTH)(?!))|\w+ – Qqbt Nov 21 '15 at 14:28