2017-03-07 77 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": "" } }'