0

I am checking for a solution to the following problem. I have a text sequence as follows and I would like to extract the contents of the square brackets which is closer to the <em> tag.

[P1/1]0(4)0(5)**[P1/432]** g(5)I(2)d(7)a(8)`<em>`b(5)[P1/4]C(6)e(7)B(8)B`</em>`(9)[P1/5]0(6)i(7)[P1/6]0(1)I(2)[P1/7]0(6)[P1/1]0(1)0(2)[P1/2]E(1)c(2)d(3)a(4)**[P1/3]** 0(1)`<em>`b(2)[P1/4]C(1)e(2)B(3)B`</em>`(4)[P1/5]0(1)

So in the above mentioned text, what I am searching for is [P1/432] and [P1/3].

With regular expression ((.(?!\[.*?]))+?)<em>, I am not able to get only the contents of the brackets, but everything from [ to <em>.

Can someone help me ??

Jan
  • 42,290
  • 8
  • 54
  • 79
San
  • 1
  • Isn't `[P1/4]` closer than `[P1/3]` to `` on counting characters between? – revo Nov 10 '17 at 14:13
  • Thanks a lot. I was searching for the content to the left side of tag (I forgot to mention). – San Nov 10 '17 at 14:43

1 Answers1

1

There is a straightforward solution if we don't care about nested, unbalanced brackets:

\[[^\]\[]*\](?=[^\]\[]*<em>)

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117