Search code examples
sqlregexnotepad++

Make all lowercase except text between speech marks


Taking this simple sample SQL:

WITH my_data as
    (SELECT 'XX_abc_123' label from DUAL UNION ALL
     SELECT 'XX_SWU_541324_FFF' from DUAL)
SELECT label from MY_DATA;

Is there a way to write a Regular Expression in Notepad++ to make all text lower case, but to not change the case of any text between the '' marks, resulting in this:

with my_data as
    (select 'XX_abc_123' label from dual union all
     select 'XX_SWU_541324_FFF' from dual)
select label from my_data;

I have tried this:

  1. Find: ([a-z])
  2. Replace: \L$1\E

But that makes everything lowercase:

with my_data as
    (select 'xx_abc_123' label from dual union all
     select 'xx_swu_541324_fff' from dual)
select label from my_data;

Solution

  • You can use

    Find What: '[^']*'|([A-Z])
    Replace With: (?1\L$1:$0)

    Make sure you select Regular expression and Match Case.

    Details:

    • '[^']*' - matches ', zero or more chars other than ' and then a '
    • | - or
    • ([A-Z]) - capture into Group 1 an ASCII uppercase letter.

    The (?1\L$1:$0) means that the replacement is a lowercased Group 1 value if Group 1 matched, else, the replacement is the whole match.

    See the demo screenshot:

    enter image description here