2013-03-22 45 views
3

我一直在學習ksh很長一段時間,但我仍然無法理解手冊中的%(pattern-pair(s))。任何人都可以舉一個簡單有意義的例子?%(模式對(s))?

A pattern of the form %(pattern-pair(s)) is a sub-pattern that can be 
used to match nested character expressions. Each pattern-pair is a two 
character sequence which cannot contain & or |. The first pattern-pair 
specifies the starting and ending characters for the match. Each sub- 
sequent pattern-pair represents the beginning and ending characters of 
a nested group that will be skipped over when counting starting and 
ending character matches. The behavior is unspecified when the first 
character of a pattern-pair is alpha-numeric except for the following: 
     D  Causes the ending character to terminate the search for 
       this pattern without finding a match. 
     E  Causes the ending character to be interpreted as an 
       escape character. 
     L  Causes the ending character to be interpreted as a quote 
       character causing all characters to be ignored when look- 
       ing for a match. 
     Q  Causes the ending character to be interpreted as a quote 
       character causing all characters other than any escape 
       character to be ignored when looking for a match. 
+0

哈哈,我一直溫和好奇這多年,從來沒有發現_anybody_它知道它。沒有一個例子,或者在任何地方都沒有提及。我們推斷這是解析諸如嵌套引號或parens之類的東西。 – ormaaj 2013-03-22 23:44:25

回答

0

我想這是對JSON解析有用:

json='{"foo":{"bar":"baz"} }' 

#remove all quoted values nested in punctuation except the first 
jsonroot=${json%%[[:punct:]][[:punct:]]%(\"\")*} 

#remove initial curly 
jsonroot=${jsonroot#?}