How to add new lines after some specific characters for a big string (1M)?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I have a string of ~1M characters, I need to add new lines after some strings BUT under certain conditions
so for example for a short string like this one:
stringOfCharacters = '{"lines": [{"product-id": 5, "name": "378-1513", "structure-name": "Subthalamic nucleus", "transgenic-line": "", "gender": "M", "injection-volume": "0.01378", "density": 0.0402453, "structure-abbrev": "STN", "strain": "C57BL/6J", "injection-coordinates": [7220, 5410, 7300], "injection-structures": [{"abbreviation": "STN", "color": "F2483B", "id": 470, "name": "Subthalamic nucleus"}, {"abbreviation": "GPi", "color": "8599CC", "id": 1031, "name": "Globus pallidus, internal segment"}], "path": [{"intensity": 0, "coord": [2600, 3400, 8000], "density": 0},'
I would like to receive something like this:
newString =
{
"lines":
[
{
"product-id": 5,
"name": "378-1513",
"structure-name": "Subthalamic nucleus",
"transgenic-line": "",
"gender": "M",
"injection-volume": "0.01378",
"density": 0.0402453,
"structure-abbrev": "STN",
"strain": "C57BL/6J",
"injection-coordinates": [7220, 5410, 7300],
"injection-structures":
[
{
"abbreviation": "STN", "color": "F2483B", "id": 470, "name": "Subthalamic nucleus"
},
{
"abbreviation": "GPi",
"color": "8599CC",
"id": 1031,
"name": "Globus pallidus, internal segment"
}
],
"path":
[
{
"intensity": 0,
"coord": [2600, 3400, 8000],
"density": 0
},
This are the conditions:
- new lines after the characters []{},
- if there are numbers and commas between [] like
[7220, 5410, 7300]
do not add a new line
- if there is a comma between "" like
"Globus pallidus, internal segment"
do not add a new line
- new line after the string:
"lines":
- after the characters
"density: #"
where # could be any number, add a new line.
I also would like to do it with the less code possible, Thanks!
0 Commenti
Risposte (1)
Guillaume
il 2 Nov 2016
You tagged your question with regexp. Unfortunately, it's not going to be possible with a regular expression. You're going to have to write your own parser as your rules are based on the semantic of the strings.
Or since it looks like it is json, find a json beautifier library, if it exists.
0 Commenti
Vedere anche
Categorie
Scopri di più su JSON Format in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!