简体   繁体   中英

Oracle Regexp_replace string

Let's say I have the following string:

 notNull(devhx_8_other2_name_2) AND notNull(devhx_8_other2_amt) 

How can I use regexp_replace to change it to:

 (devhx_8_other2_name_2) is not null AND (devhx_8_other2_amt) is not null 

Use

regexp_replace(col, 'notNull(\([^)]+\))', '\1 is not null', 1, 0)

This looks for 'notNull' followed immediately by an opening parenthesis, other characters and a closing parenthesis. It replaces this with the string including the parentheses, but without 'notNull' and appends 'is not null'.

You can use the pattern:

  • notNull - match the string
  • ( - start a capture group
  • \\(.+?\\) - match an opening bracket then one-or-more characters but as few as possible until it matches a closing bracket
  • ) - end of the capture group.

And then replace it with \\1 is not null which will substitute \\1 for the value matched in the first capture group. Like this:

SELECT REGEXP_REPLACE(
         your_column,
         'notNull(\(.+?\))',
         '\1 is not null'
       )
FROM   your_table

假设您的字符串始终采用您显示的格式,则不需要正则表达式:

replace( replace( yourString, ')', ') is not null '), 'notNull', '')

Use the following regexp_replace function:

regexp_replace(regexp_replace(string_name,"notNull",""),"')","') is not null")

Here I replace the 'notNull' with a non space ie '' and then replace the closing bracket ie ')' with a closing bracket, a space and the text 'is not null'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM