Skip to main content
added 111 characters in body
Source Link
lawlist
  • 19.7k
  • 5
  • 44
  • 129

I am struggling to come up with a universal regexp that will remove/isolate all leading/trailing garbage from a multi-line string, leaving only the JSON. Without opening up a temporary buffer to re-search-forward/backward for the first and last wavy brackets, how can I programmatically remove everything before the first { and everything after the last }?

CAVEAT: Sometimes, there may not necessarily be any leading/trailing garbage, but there often will be.

BEFORE

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. [Donec hendrerit
tempor tellus.]

Donec pretium posuere tellus. Proin quam nisl, tincidunt et,
mattis eget, convallis nec, purus.  {Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.} Nulla posuere. Donec vitae

dolor."

AFTER

"{Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.}"

I am struggling to come up with a universal regexp that will remove/isolate all leading/trailing garbage from a multi-line string, leaving only the JSON. Without opening up a temporary buffer to re-search-forward/backward for the first and last wavy brackets, how can I programmatically remove everything before the first { and everything after the last }?

BEFORE

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. [Donec hendrerit
tempor tellus.]

Donec pretium posuere tellus. Proin quam nisl, tincidunt et,
mattis eget, convallis nec, purus.  {Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.} Nulla posuere. Donec vitae

dolor."

AFTER

"{Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.}"

I am struggling to come up with a universal regexp that will remove/isolate all leading/trailing garbage from a multi-line string, leaving only the JSON. Without opening up a temporary buffer to re-search-forward/backward for the first and last wavy brackets, how can I programmatically remove everything before the first { and everything after the last }?

CAVEAT: Sometimes, there may not necessarily be any leading/trailing garbage, but there often will be.

BEFORE

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. [Donec hendrerit
tempor tellus.]

Donec pretium posuere tellus. Proin quam nisl, tincidunt et,
mattis eget, convallis nec, purus.  {Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.} Nulla posuere. Donec vitae

dolor."

AFTER

"{Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.}"
Source Link
lawlist
  • 19.7k
  • 5
  • 44
  • 129

Extract JSON from multi-line string, removing leading/trailing garbage

I am struggling to come up with a universal regexp that will remove/isolate all leading/trailing garbage from a multi-line string, leaving only the JSON. Without opening up a temporary buffer to re-search-forward/backward for the first and last wavy brackets, how can I programmatically remove everything before the first { and everything after the last }?

BEFORE

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. [Donec hendrerit
tempor tellus.]

Donec pretium posuere tellus. Proin quam nisl, tincidunt et,
mattis eget, convallis nec, purus.  {Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.} Nulla posuere. Donec vitae

dolor."

AFTER

"{Cum sociis natoque [penatibus et] {magnis
dis} parturient montes, {nascetur ridiculus} mus.}"