Sublime Text - 将文本转换为 JSON 格式
Sublime Text - Convert text to JSON format
我有一个与此类似的文本文件:
Reference: 10001
Title: This is a text file
Description: A text file containing some simple strings
<0x0c>
Reference: 10002
Title: This is an xml file
Description: An xml file containing some generic data
<0x0c>
基本上,我看到像 <word>:
这样的模式,我想在 <word>
前后插入 "
,然后我想在 "
前后插入 "
值后跟 ,
,如下所示:
"Reference" : "10001",
到目前为止,我可以使用以下正则表达式获得整行:
^.*(Reference:|Title:).*$
但我想知道是否有更好的方法来 select 仅我需要的文本,然后进行所需的转换。
如果你不想匹配整行而只想匹配文本,你可以使用 3 个捕获组并使用单词边界 \b
:
\b(Reference|Title)(:\s*)(\d+)\b
\b(Reference|Title)
字边界,在组 1 中捕获参考或标题
(:\s*)
在组 2 中捕获一个冒号和 0+ 个空白字符
(\d+)\b
在组 3 中捕获 1+ 数字后跟单词边界
在替换中使用 3 个捕获组并用双引号括起第一个和第三个:
""""
我有一个与此类似的文本文件:
Reference: 10001
Title: This is a text file
Description: A text file containing some simple strings
<0x0c>
Reference: 10002
Title: This is an xml file
Description: An xml file containing some generic data
<0x0c>
基本上,我看到像 <word>:
这样的模式,我想在 <word>
前后插入 "
,然后我想在 "
前后插入 "
值后跟 ,
,如下所示:
"Reference" : "10001",
到目前为止,我可以使用以下正则表达式获得整行:
^.*(Reference:|Title:).*$
但我想知道是否有更好的方法来 select 仅我需要的文本,然后进行所需的转换。
如果你不想匹配整行而只想匹配文本,你可以使用 3 个捕获组并使用单词边界 \b
:
\b(Reference|Title)(:\s*)(\d+)\b
\b(Reference|Title)
字边界,在组 1 中捕获参考或标题(:\s*)
在组 2 中捕获一个冒号和 0+ 个空白字符(\d+)\b
在组 3 中捕获 1+ 数字后跟单词边界
在替换中使用 3 个捕获组并用双引号括起第一个和第三个:
""""