2017-03-07 169 views
1

所有我的大学笔记JSON格式,当我得到一组的实际问题,从一个PDF它的格式是这样的:转换纯文本与VIM特定的格式转换成JSON

 
1. Download and compile the code. Run the example to get an understanding of how it works. (Note that both 
threads write to the standard output, and so there is some mixing up of the two conceptual streams, but this 
is an interface issue, not of concern in this course.) 
2. Explore the classes SumTask and StringTask as well as the abstract class Task. 
3. Modify StringTask.java so that it also writes out “Executing a StringTask task” when the execute() method is 
called. 
4. Create a new subclass of Task called ProdTask that prints out the product of a small array of int. (You will have 
to add another option in TaskGenerationThread.java to allow the user to generate a ProdTask for the queue.) 
Note: you might notice strange behaviour with a naïve implementation of this and an array of int that is larger 
than 7 items with numbers varying between 0 (inclusive) and 20 (exclusive); see ProdTask.java in the answer 
for a discussion. 
5. Play with the behaviour of the processing thread so that it polls more frequently and a larger number of times, 
but “pop()”s off only the first task in the queue and executes it. 
6. Remove the “taskType” member variable definition from the abstract Task class. Then add statements such as 
the following to the SumTask class definition: 
private static final String taskType = "SumTask"; 
Investigate what “static” and “final” mean. 
7. More challenging: write an interface and modify the SumTask, StringTask and ProdTask classes so that they 
implement this interface. Here’s an example interface: 

我会喜欢做的是将它复制到vim和执行的查找和替换将其转换成这样:


    "1": { 
     "Task": "Download and compile the code. Run the example to get an understanding of how it works. (Note that both threads write to the standard output, and so there is some mixing up of the two conceptual streams, but this is an interface issue, not of concern in this course.)", 
     "Solution": "" 
    }, 
    "2": { 
     "Task": "Explore the classes SumTask and StringTask as well as the abstract class Task.", 
     "Solution": "" 
    }, 
    "3": { 
     "Task": "Modify StringTask.java so that it also writes out “Executing a StringTask task” when the execute() method is called.", 
     "Solution": "" 
    }, 
    "4": { 
     "Task": "Create a new subclass of Task called ProdTask that prints out the product of a small array of int. (You will have to add another option in TaskGenerationThread.java to allow the user to generate a ProdTask for the queue.) Note: you might notice strange behaviour with a naïve implementation of this and an array of int that is larger than 7 items with numbers varying between 0 (inclusive) and 20 (exclusive); see ProdTask.java in the answer for a discussion.", 
     "Solution": "" 
    }, 
    "5": { 
     "Task": "Play with the behaviour of the processing thread so that it polls more frequently and a larger number of times, but “pop()”s off only the first task in the queue and executes it.", 
     "Solution": "" 
    }, 
    "6": { 
     "Task": "Remove the “taskType” member variable definition from the abstract Task class. Then add statements such as the following to the SumTask class definition: private static final String taskType = 'SumTask'; Investigate what “static” and “final” mean.", 
     "Solution": "" 
    }, 
    "7": { 
     "Task": "More challenging: write an interface and modify the SumTask, StringTask and ProdTask classes so that they implement this interface. Here’s an example interface:", 
     "Solution": "" 
    } 

试图在实际(而不是实际做实用)算出这个之后,这是我得到的最接近:


%s/\([1-9][1-9]*\)\. \(\_.\{-}\)--end--/"\1": {\r "Task": "\2",\r"Solution": "" \r},/g 

的3个问题与此有

  1. 我要--end--添加到每个问题的结束。我希望通过展望以[1-9] [1-9] *开头的行来知道问题何时结束。不幸的是,当我搜索它时,它也取代了这一部分。
  2. 这会保留问题中的所有新行(在JSON中无效)。我希望它删除新的行。
  3. 中的最后一项不应该包含“”的输入之后,因为这也将是无效的JSON(注意:我不介意这个非常,因为它很容易删除最后一个“”手动)

请记住我在正则表达式中非常糟糕,我这样做的原因之一就是要了解更多关于正则表达式的知识,所以请解释任何您作为解决方案发布的正则表达式。

+0

这些线是由分隔'\ N'? –

+0

@SahilGulati是的。 – KNejad

回答

2

在两个步骤:

%s/\n/\ /g 

解决问题2,然后:

%s/\([1-9][1-9]*\)\. \(\_.\{-}\([1-9][1-9]*\. \|\%$\)\@=\)/"\1": {\r "Task": "\2",\r"Solution": "" \r},\r/g 

解决问题1. 可以解决问题3替换另一个轮。另外,我的解决方案在任务条目的末尾插入不需要的额外空间。尝试自己删除它。

我已经添加了什么简短的解释:

\|:或;

\%$:文件结束;

\@=:找到但不包含在匹配中。

+0

感谢@mbjoe这几乎是完美的。我唯一的问题是,如果一个问题包含“1.”,那么它会将其视为一个单独的问题。例如,如果问题包括“问题1中的问题”。那么它将在“1”处再次分裂。是什么使得它清楚地表明这是一个问题,这个1.就在这条线的起点。您在第一次查找替换时删除的内容。出于好奇,是否有可能一劳永逸而不是将它分成2个?因为我的一位朋友说这是不可能的,但我觉得必须有办法。这个\ @ =正是我正在寻找的东西。 – KNejad

+0

关于“1.”在问题中,'[1-9]'之前的'^'只在行的开头搜索。但是,在第一步移除所有换行符后,就无法使用它了。解决办法是让第一步变得更加复杂,并避免在下一个问题之前删除换行符,可能使用'\ @!'(反向匹配)来避免换行符后跟'^ [1-9]'。关于一气呵成,我觉得这很困难,尽管我不确定这是不可能的。 – mbjoe

+0

我试着解决我提到的问题,现在我将它添加到我的'.vimrc'中:'map :%s/\ n \([0-9] \ + \。\)\ @!/ \/g :%s/\([0-9] \ + \)。 \(\ _。\ { - } \)\ n /“\ 1”:{\ r“Task”:“\ 2”,\ r“Solution”:“”\ r},\ r/g '。所以按下我的键盘上的ctrl-f会自动将它格式化为JSON。注意我也用[0-9] +代替了所有的[1-9] [1-9] *,这样它也可以找到其中有0的数字 – KNejad

1

如果每个项目坐落在单行线,我将改变与宏文本,它比:s更短,更直接:

I"<esc>f.s": {<enter>"Task": "<esc>A"<enter>"Solution": ""<enter>},<esc>+ 

记录这个宏在寄存器中,像q,那么你就可以只是重播它像[email protected]做转型。

注意

  • 结果会留下一个逗号,和结束,只是将其删除。
  • 您还可以在宏记录期间添加缩进,然后您的json将会“漂亮地打印”。或者你可以用其他工具稍后做出性感。
+0

谢谢Kent,你的答案有两个问题:1)问题跨度多行。所以按A 它不会达到问题的结尾。 2)这是我的错。我不认为我在这个问题中已经明确表达出来了,但是我想用查找和替换来做到这一点,因为这对我来说是一个非常有用的练习,因为我相信这是一个非常强大的东西,我想学习。这就是说你的答案非常接近,所以我会投票。 (不幸的是,因为我不到50个代表,我的积分不会被记录) – KNejad

1

你或许可以用一个大的正则表达式做到这一点,但这很快就变得无法维护。我会将任务分成3个步骤:

  1. 将每个编号的步骤分成它自己的段落。
  2. 把每个段落放在一行上。
  3. 生成JSON。

综上所述:

%s/^[0-9]\+\./\r&/ 
%s/\(\S\)\n\(\S\)/\1 \2/ 
%s/^\([0-9]\+\)\. *\(.*\)/"\1": {\r "Task": "\2",\r "Solution": ""\r},/ 

该解决方案还使最后一个元素之后逗号。

$s/,// 

说明

  • %s/^[0-9]\+\./\r&/此相匹配的线开始以数字后跟一个点,例如:这可以通过去除1.,8.,13.,131等,并用换行符(\r)替换后再匹配(&)。
  • %s/\(\S\)\n\(\S\)/\1 \2/这将删除任何两侧都带有非空格字符的换行符(\S)。
  • %s/^\([0-9]\+\)\. *\(.*\) ...捕获\1\2中的数字和文字。
  • ... /"\1": {\r "Task": "\2",\r "Solution": ""\r},/适当地格式化文本。

使用SED,AWK和JQ

可以执行步骤1和2从上面直截了当地用sedawk替代方式:

  1. sed 's/^[0-9]\+\./\n&/' infile
  2. awk '$1=$1; { print "\n" }' RS= ORS=' '

使用jq为第三步骤确保输出是有效的JSON:

  • jq -R 'match("([0-9]+). *(.*)") | .captures | {(.[0].string): { "Task": (.[1].string), "Solution": "" } }'
  • 在这里作为一个命令行:

    sed 's/^[0-9]\+\./\n&/' infile   | 
    awk '$1=$1; { print "\n" }' RS= ORS=' ' | 
    jq -R 'match("([0-9]+). *(.*)") | .captures | {(.[0].string): { "Task": (.[1].string), "Solution": "" } }'