使用Perl,我試圖替換所有圍繞由空格分隔的單詞的括號。例如,字符串:正則表達式刪除由空格分隔的字符周圍的括號
$string = "A string (with words) is always easy to understand Ref: A(1,2)";
應該成爲
$string = "A string with words is always easy to understand Ref: A(1,2)";
我可以拿起匹配使用lookarounds,但不能確定如何更換括號,而不是字,即
$string =~ s/(?<=\s\()\w*(?=\)\s)//g;
會給
$string = "A string() is always easy to understand Ref: A(1,2)";
是否可以設置一個正則表達式來替換搜索結果而不是匹配?或者我在想這個錯誤?
太棒了!新的正則表達式,並要瞭解現在捕獲組。 –
@JamesB:你可以按照我剛剛在我的回答中添加的鏈接。 –
@Casimir et Hippolyte也許這會更好地問一個新的問題,但你將如何處理嵌套圓括號組,即「」一個字符串(有(更多)字)總是(容易)理解Ref:A(1,2 )「'導致''帶有更多單詞的字符串總是容易理解Ref:A(1,2)」'。 – Chris