1
我有以下文本:捕獲括號匹配的序列中的內容與正則表達式表達
y=asin(pi*2*(vel_rot+(dan_int+didt)/2.));
我希望有一個正則表達式表達捕獲參數的函數ASIN,即,在該示例中,它應該比賽:
pi*2*(vel_rot+(dan_int+didt)/2.)
我的問題是,我不知道如何我覺得
我有以下文本:捕獲括號匹配的序列中的內容與正則表達式表達
y=asin(pi*2*(vel_rot+(dan_int+didt)/2.));
我希望有一個正則表達式表達捕獲參數的函數ASIN,即,在該示例中,它應該比賽:
pi*2*(vel_rot+(dan_int+didt)/2.)
我的問題是,我不知道如何我覺得
使用PCRE(或Perl)風格的引擎,支承實跳過儘可能多的右括號爲左括號遞歸。
或者,您可以使用Dot-Nets計數組來導航嵌套。
這是前者。
y=asin(\(((?:[^()]++|(?1))*)\))
解釋
y=asin
( # (1 start), Recursion code group
\(
( # (2 start), Capture, inner core
(?: # Cluster group
[^()]++ # Possesive, not parenth's
| # or,
(?1) # Recurse to group 1
)* # End cluster, do 0 to many times
) # (2 end)
\)
) # (1 end)
輸出
** Grp 0 - (pos 0 , len 40)
y=asin(pi*2*(vel_rot+(dan_int+didt)/2.))
** Grp 1 - (pos 6 , len 34)
(pi*2*(vel_rot+(dan_int+didt)/2.))
** Grp 2 - (pos 7 , len 32)
pi*2*(vel_rot+(dan_int+didt)/2.)
如果您正在使用perl的,怎麼樣** Y = ASIN [((。*))] **?您可以捕獲括號內容。這是否出現在單行中?如果它發生在某些周圍環境中,則括號可能不正確。 – blackpen
平衡圓括號是無法用純正則表達式解決的問題的標準示例。有些語言支持擴展正則表達式,允許這樣的事情,但有一些原因需要用正則表達式來完成嗎?一個非常簡單的循環和計數器將是您試圖完成的一個簡單而有效的解決方案。 –
@JimLewis我同意。有一些編輯雖然提供了只接受正則表達式的find/substitute工具。使用正則表達式可以節省執行時間 – AndresR