文の中で括弧内の単語に一致する正規表現が必要です。例えば:
"this is [stack]overflow. I [[love]this[website]]."
上記の文から一致させたいのは、stack、love、website という単語です。
次の正規表現を試しましたが、うまくいき\[(.*[^\]\[])\]
ません。
文の中で括弧内の単語に一致する正規表現が必要です。例えば:
"this is [stack]overflow. I [[love]this[website]]."
上記の文から一致させたいのは、stack、love、website という単語です。
次の正規表現を試しましたが、うまくいき\[(.*[^\]\[])\]
ません。
以下が機能するはずです。
\[([^\[\]]*)\]
例: http://www.rubular.com/r/uJ0sOtdcgF
説明:
\[ # match a literal '['
( # start a capturing group
[^\[\]]* # match any number of characters that are not '[' or ']'
) # end of capturing group
\] # match a literal ']'
シェルでこれを試してください:
$ echo 'this is [stack]overflow. I [[love]this[website]]' |
grep -oP '\[+\K[^\]]+'
stack
love
website
これは、PCRE および perl エンジンで動作します。
説明
\[ # match a literal '['
+ # one (preceding character) or more
\K # "reset" the regex to null
[^] # excluding class, here a literal \]
\] # match a literal ']'