簡體   English   中英

RegEx以[和結尾]開頭 - 僅匹配單詞

[英]RegEx Starts with [ and ending with ] - match words only

鑒於以下條紋:

var s = "my [first] ga[m]e and [abc]de]\nThe [Game]"

我使用哪個正則表達式匹配:

0 - [first]
1 - [abc]de]
2 - [Game]

我試過var pattern2 = new Regex(@"\\W\\[.*?\\]\\W"); 但它找不到[Game]

我也希望能夠匹配“ [my] gamers\\t[pro]

0 - [my]
1 - [pro]
\[[^\[]{2,}\]

說明:

\[         # Match a [
[^\[]{2,}  # Match two or more non-[ characters
\]         # Match ]

RegExr上看到它。

除了非單詞字符外,您還需要顯式匹配字符串的開頭和結尾:

(?:^|\W)\[(.*?)\](?:$|\W)

捕獲組將獲取括號內的單詞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM