1

Him I would like to split string by two characters.

For example I have string like this one: "xx-aa-[aa]-22-[bb]". I want to retrieve string array of [aa] and [bb]. All characters between [ ].

First I can split by '-', so I'll have string array

var tmp = myString.Split('-');

But now how can I retrieve only strings between [] ?

Nayeem Mansoori
  • 771
  • 1
  • 14
  • 39
mskuratowski
  • 3,844
  • 9
  • 51
  • 104

1 Answers1

8

You can use following regex:

\[(.+?)\]

Use global flag to match all the groups.

Demo

Explanation

  1. (): Capturing Group
  2. \[: Matches [ literal. Need to escape using \
  3. .+?: Non-greedy match any number of any characters
  4. \]: Matches ] literal. Need to escape using \

Visualization

enter image description here

Tushar
  • 82,599
  • 19
  • 151
  • 169