簡體   English   中英

REGEXP_SUBSTR:提取[]包括[]之間的字符串部分

[英]REGEXP_SUBSTR : extracting portion of string between [ ] including []

我在Oracle 11gR2上。

我試圖在'['和']'之間提取文本,包括[]。

例如:

    select regexp_substr('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]','\[(.*)\]') from dual

輸出:

    [REQ.UID] and username=[REQD.VP.UNAME]

需要的輸出:

    [REQ.UID][REQD.VP.UNAME]

請讓我知道如何獲得所需的輸出。

謝謝和問候,Bishal

假設您將要出現兩次[],那么以下內容就足夠了。 的? 在里面 。*? 意味着它是非貪婪的,所以它不會吞噬最后一個]。

select
 regexp_replace('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]'
,'.*(\[.*?\]).*(\[.*?\]).*','\1\2')
from dual
;

我不是Oracle用戶,但是通過快速瀏覽文檔 ,我認為這應該是關閉的:

REGEXP_REPLACE('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]',
               '^[^\[]*(\[[^\]]*\])[^\[]*(\[[^\]]*\])$', '\1 \2')

這看起來比實際上更糟糕。

模式是:

  • ^[^\\[]*捕獲所有字符(但不包括)第一個[
  • (\\[[^\\]]*\\])捕捉到第1組,如[<not "]">]
  • [^\\[]*捕獲所有內容(堅果不包括)下一個[
  • (\\[[^\\]]*\\])在字符串末尾捕獲類似於[<not "]">]組2

然后更換很簡單,只需<grp 1> <grp 2>

暫無
暫無

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

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