Pregunta

I'm trying to clean a huge geoJson datafile. I need to change the format of "text" field from

"text": "(2:Placename,Placename)"

to

"text": "Placename".

In Sublime text I managed to write a regular expression which enabled me to select and remove the first part leaving something like this:

"text": "Placename)"

With following regexp I can select the text above, but I need to narrow it down to the last character:

text\": \".*?\)

No matter what I can't figure out how to select the ")" character in the end of Placename string in the whole file and remove it. Note that the "Placename" here can be any place name, like New York, London etc.

I tried to build an expression where first part finds the text field, then ignores n-amount of characters until it finds the ")" character. After experimenting and Googling I couldn't find a solution here.

¿Fue útil?

Solución

You can capture the value of the second placemark field with the following regexp:

/"text": "+\(\d+:[^,]+,(.*?)\)/

Which will capture "Placename" in $1

More info on capturing parenthesis: http://www.regular-expressions.info/brackets.html

The trick is to use the inverted character classes and to escape any parentheses you want to match.

HTH

Otros consejos

I do not know if you are using a Unix system, but probably sed can do much of the work for you. It can interpret regular expressions, capture groups, and substitute by other groups of characters. I have tried an example with sed and the following sed command worked for me:

echo "\"text\": \"(2:Placename,Placename)\"" | sed -r 's/(\"text\": )\"\([[:digit:]]:[^0-9]+,([^0-9]+)\)\"/\1\"\2\"/g'

-r allows sed to interpret regular expressions. I am using parentheses to capture groups that I will use later in the substitution (e.g., a group for "text", and a group for the second placename). In the substitution part of sed, you can use groups by using \n where n is the group number that you want to used. This expression should help you to achieve your desired result.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top