Regular Expressions 101

Community Patterns

Look up corresponding value in unsorted list

1

Regular Expression
PCRE (PHP <7.3)

/
(?<="name": ")(.+)(",\n\s+"war_memorials": ")(.*?\1,)(.*?)(,.*)(?=",)
/
gm

Description

We have a GeoJSON that needs a "war_memorials" attribute added and populated with the correct value.

Trouble is the sorting of the Features is not the same as the sorting of the CSV data with the values. So you can't just use a multiple-selection in Sublime Text and paste in place.

This solution takes all the CSV data and smushes it onto a long line: Name1,Value1,Name2,Value2, and so on. You paste this long line as the new attribute's value in all of the Features.

Then using this regular expression you can reduce the line to the value corresponding to the name above. The replacement pattern removes other extraneous name-value pairs.

Submitted by anonymous - 6 years ago