4

I'm using Notepad++ to find/replace and can't quite get what I need.

My Current Output is being generated with ^(.+)$ as my Find and ,dim_date[(\1)].alias\((\1)\) as my Replace. It's close, but I can't get the uppercase between the parenthesis as it is in the Desired Output.

If I can alter my find/replace to do it all in one step, that's cool; but I don't mind keeping my first find/replace and then performing another find/replace to get the snake case capitalized. Whatever is easier.

Sample Input:

'business_year'
'business_year_month'
'business_year_quarter'

My Current Output:

,dim_date['business_year'].alias('business_year')
,dim_date['business_year_month'].alias('business_year_month')
,dim_date['business_year_quarter'].alias('business_year_quarter')

Desired Output:

,dim_date['business_year'].alias('Business_Year')
,dim_date['business_year_month'].alias('Business_Year_Month')
,dim_date['business_year_quarter'].alias('Business_Year_Quarter')
2
  • Here's one way that can be done in Ruby. I mention it should there be a way to implement the same idea in Notepad++. First, create a hash (dictionary) that maps lowercase letters to their uppercase equivalent: h = ('a'..'z').zip('A'..'Z').to_h #=> {"a"=>"A", "b"=>"B", ... , "z"=>"Z"}. E.g., h['b'] #=> 'B'. Then make use of the second form of the method String#gsub... Commented Jun 19 at 7:27
  • ...If str = 'business_year_quarter', ",dim_date['#{str}'].alias('#{str.gsub(/(?<=_)[a-z]/, h)}'\)" #=> ",dim_date['business_year_quarter'].alias('business_Year_Quarter')". Commented Jun 19 at 7:29

3 Answers 3

5
  • Ctrl+H
  • Find what: '([a-z]+)(?:_([a-z]+)(?:_([a-z]+))?)?'
  • Replace with: ,dim_date[$0].alias\('\u$1(?2_\u$2(?3_\u$3))'\)
  • TICK Match case
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

'               # single quote
([a-z]+)        # group 1, 1 or more lowercase
(?:             # non capture group
    _               # underscore
    ([a-z]+)        # group 2, 1 or more lowercase
    (?:             # non capture group
        _               # underscore
        ([a-z]+)        # group 3, 1 or more lowercase
*** you can add as many groups as needed
    )?              # end group, optional
)?              # end group, optional
'               # single quote

Replacement:

,dim_date[      # literally
$0              # the whole match
].alias\('      # lierally
\u$1            # uppercase the first letter of group 1
(?2             # if group 2 exists
    _\u$2           # underscore and upper the first letter of group 2
    (?3             # if group 3 exists
        _\u$3           # underscore and upper the first letter of group 3
    )               # endif
)               # endif
'\)             # literally

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

1
  • Thanks for adding the detailed explanation on this. Helps with future needs
    – simplicITy
    Commented Jun 18 at 17:09
4

If you need this for an arbitrary amount of key_ parts this worked for me. Search for:

^(?=(.+))|([a-z]+_?)(')?

And replace with:

(?1,dim_date[$1].alias\(:\u$2(?3'\)))

The left part of the alternation is for capturing the full line (what will be to the left of your period) without consuming it. The right side matches each key_ part for capitalizing the first letter by use of \l. The last group captures a single quote at the end to check for this condition later.

In the replacement conditional will be checked, if the first group matched. If so, the captured value gets inserted into the dim_date[ $1 ].alias\( :else \u will capitalize the first letter of each key-part captured by $2. The third group is for closing with '\) if a single-quote was matched.

It's supposed to work like this slightly different PCRE2 demo (not working in Notepad++). Certainly doing just two simple replacements is more recommended, but I was curious if this works.

0
2

In two steps (from the sample input):

'business_year'
'business_year_month'
'business_year_quarter'

First find ('|_)(\w) replace by $1\U$2\E:

'Business_Year'
'Business_Year_Month'
'Business_Year_Quarter'

Then find ^(.+)$ replace by ,dim_date[(\L$1\E)].alias\($1\):

,dim_date['business_year'].alias('Business_Year')
,dim_date['business_year_month'].alias('Business_Year_Month')
,dim_date['business_year_quarter'].alias('Business_Year_Quarter')

Not the answer you're looking for? Browse other questions tagged or ask your own question.