开发者

How to convert non-formats to JSON with Find/Replace?

开发者 https://www.devze.com 2023-02-17 18:34 出处:网络
How can I use Find & Replace (or Regex or Vim or whatever) to convert the following bunch: 1 string one

How can I use Find & Replace (or Regex or Vim or whatever) to convert the following bunch:

1
string one
2
string two
3
stirng three
4
string f开发者_开发技巧our
.
.
.

to a JSON format, like so:

1: { 1: 'string one' },
2: { 1: 'string two' },
3: { 1: 'string three' },
4: { 1: 'string four' }
.
.
.

Any ideas?

Thanks!


Using vim:
1-) I would record a macro to put key and content in the same line (couldn't find how to apply regex to multiple lines)
2-)%s/\([0-9]\+\) \(.*\)/\1: {'\1':\'\2'},/g
The first part (0-9)+ will get the index, the second part (.*) will get the rest of the string.

Then you can pretty much what you want with it. In this example, I'm following your syntax

key: {'key':'value'},

In this case, you will have a comma in the last line, but it should not be a problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消