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
  • 821
  • 1
  • 15
  • 41
mskuratowski
  • 4,014
  • 15
  • 58
  • 109

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
  • 85,780
  • 21
  • 159
  • 179