0

I have the following text:

  [[{"type":"media","view_mode":"media_original","fid":"19125","attributes":{"alt":"","class":"media-image","height":"480","typeof":"foaf:Image","width":"480"}}]]

Sed iaculis, nisl vel accumsan condimentum, orci felis congue pede, nec pharetra quam ante nec ligula.

  [[{"type":"media","view_mode":"media_original","fid":"16145","attributes":{"alt":"","class":"media-image","height":"480","typeof":"foaf:Image","width":"480"}}]]

I want to select the first paragraph of the text (section in brackets), based on the fid (19125).

This is my current regexp:

[\[].[^;]*[\]\)]]

But right now it will find both occurrences. I'd like to adjust this so it only gets the one with the correct fid (19125).

4
  • Why not just use json_decode and get the value? Commented Nov 21, 2017 at 15:28
  • @ctwheels I think the OP means the whole code block (two JSON strings and the text between them) is the input. Commented Nov 21, 2017 at 15:29
  • @mingos that makes sense, thanks for clarifying Commented Nov 21, 2017 at 15:29
  • You could just use \[{2}{.*"fid"\s*:\s*"19125".* Commented Nov 21, 2017 at 15:33

1 Answer 1

2

You just need to add in the id to your regex. I've slightly change the regex so that [^;]* turned into [^;]*"fid":"19125"[^;]*. Which is roughly comparable to just doing a contains within each match to see if fid equals 19125.

[\[].[^;]*"fid":"19125"[^;]*[\]\)]]

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.