Response to : Regular Expression to find a string included between two characters while EXCLUDING the delimiters
Hi,I’m looking for a regex pattern that applies to my string including brackets:
[1,2,3,4,5] [abc,ef,g] [0,2,4b,y7]
could be anything including word,digit,non-word together or separated.
I wish to get the group between brackets by \[(.*?)\]
but what is the regex pattern that will give me the group between brackets and sub-group strings separated by commas so that the result may be following ??
Group1 : 1,2,3,4,5 Group1: 1 Group2: 2 Group3: 3 Group4: 4 Group5: 5 Group2 : abc,ef,g Group1: abc Group2: ef Group3: g etc ..
Thank you for your help
I agree with @Dav that you would be best using String.Split on each square-bracketed group.
However, you can extract all the data using a single regular expression:
Using this expression, you will have to process all the captures of each group to get all the data. As an example, run the following code on your string:
This produces the following output:
Group 1 gives you the value of each square-bracketed group, group 2 gives you the first item matched in each square-bracketed group and group 3 gives you all the subsequent items. You will have to look at the indexes of the captures to determine which item belongs to each square-bracketed group.