3

我已经学习 ksh 很长一段时间了,但我仍然无法%(pattern-pair(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.
4

1 回答 1

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#?}
于 2013-06-20T23:38:57.457 回答