You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求REGEXP_REPLACE正则表达式:将嵌套函数转为下划线连接形式

Hey there, let's fix that regex replacement issue you're facing. Your current approach only swaps out the opening parenthesis of func_1, but leaves an extra closing parenthesis hanging—we need to target the full wrapping pair and restructure the string properly. Also, since you mentioned there are only 5 possible values for the outer function, we can make the regex precise to avoid accidental matches elsewhere.

The Correct Regex Solution

We need to match the full pattern of your outer function wrapping an inner function, then rewrite it to concatenate the function names while keeping the inner function's content intact. Here's how to implement this in Oracle's REGEXP_REPLACE:

Breakdown of the Regex Pattern:

  • Outer Function Group: (func_1|func_2|func_3|func_4|func_5) — replace these with your actual 5 outer function names (this ensures we only target the specific functions you care about)
  • Outer Parentheses: We match the opening \( and closing \) of the outer function
  • Inner Function Capture: (func_\w+\([^)]+\)) — this grabs the entire inner function call, including its own parentheses and parameters. This works because your examples don't have nested parentheses inside the inner function's parameters.

The SQL Query

SELECT REGEXP_REPLACE(
  your_input_column, -- Replace with your actual column name or string
  '(func_1|func_2|func_3|func_4|func_5)\((func_\w+\([^)]+\))\)',
  '\1_\2'
) AS transformed_result
FROM dual;

Testing Against Your Examples:

  • Input: func_1(func_2(a,b)) → Output: func_1_func_2(a,b) ✔️
  • Input: func_1(func_2()) → Output: func_1_func_2() ✔️
  • Input: func_1(func_2()) + 3 → Output: func_1_func_2() + 3 ✔️
  • Input: func_1(func_2()) + func_1(func_3()) → Output: func_1_func_2() + func_1_func_3() ✔️

Quick Notes:

  • Make sure to update the outer function group (func_1|...|func_5) to match your actual 5 function names. If any of them have special characters (like hyphens), you'll need to escape them with a backslash (\).
  • If you ever need to handle nested parentheses inside the inner function's parameters, this regex won't work (since Oracle's regex engine doesn't support balanced parentheses natively). But based on your provided examples, this solution fits perfectly.

内容的提问来源于stack exchange,提问作者Sriram S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:26:23