簡體   English   中英

這個正則表達式匹配什么?

[英]what does this regular expression match?

我遇到了這個表達式,並試圖弄清楚正則表達式會匹配什么?

new Text(Arrays.toString(parts).replaceAll("^\\[|\\]$", "")

我使用Xeger生成一些字符串,但它可能沒有顯示正確的字符串,因為它只生成2個字符串^ [和] $

你的正則表達式的作用是匹配起始[和結尾]

工作演示

在此輸入圖像描述

正則表達式可視化

Debuggex演示

regex101的解釋:

1st Alternative: ^\[
    ^ assert position at start of a line
    \[ matches the character [ literally
2nd Alternative: \]$
    \] matches the character ] literally
    $ assert position at end of a line

所以,這段代碼:

"[sdfsad sdfsd]asdfas]".replaceAll("^\\[|\\]$", "")

將刪除開始和結束[ ] ,結果字符串將是:

sdfsad sdfsd]asdfas

暫無
暫無

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

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