3

I want to convert all the strings in my notepad/Sql Server management studio into camel case and remove all the underscores.

E.g.:

string a = REFERENCE_DATA_ID
String b = ReferenceDataId

I want to remove the underscore in string a and covert into a camel case as shown in string b. Please help me out.

Dixit Chopra
  • 77
  • 2
  • 10
  • 1
    this cannot be done with pure regex and hence not in notepad++ without some script. you need a callback function to do the conversion – Martin Ender Nov 09 '12 at 11:09
  • I want to change the script in Microsoft SQL server management studio. They provide the option to use Replace using Regular expressions. – Dixit Chopra Nov 09 '12 at 11:10
  • 1
    This sort of transformation is not generally supported by regular expression engines no matter what tool you were using. If you were using Perl, you could use the /e modifier to execute some code to transform the replacement text, but you don't have that option available. – pndc Nov 09 '12 at 11:23
  • See this stackoverflow : [Convert a char to upper case using regular expressions (EditPad Pro)][1] [1]: http://stackoverflow.com/questions/1159343/convert-a-char-to-upper-case-using-regular-expressions-editpad-pro –  Mar 01 '13 at 16:20

2 Answers2

3

You can do this with one step in notepad++:

Find: ([a-z]+)[_]?([a-z]?)([a-z]+)[_]?([a-z]?)([a-z]+)[_]?([a-z]?)([a-z]+)\.php
Replace: $1\U$2\L$3\U$4\L$5\U$6\L$7

The only issue with this, is that you need to know the max time the under score can be present and how the string ends. In the above example, i'm replacing php file names to camelCase, knowing that the under score cannot be present more than 3 times, less is no problem.

Inc33
  • 1,747
  • 1
  • 20
  • 26
  • That worked like a charm man! Out of thirty six fields, it handled them all save for 6 which had a numeric digit after the last underscore! Thank you! – JGlass Jan 15 '19 at 13:10
1

Assuming your data is already in a table, does it have to be using a reg ex? Why don't you create a scalar function to do this and follow the steps below:

  1. UPDATE tableName SET stringa = REPLACE(stringa,'_',' ') --Replace underscores with a space
  2. UPDATE tableName SET stringa = dbo.ProperCase(stringa) --update the field using the function I have created below
  3. UPDATE tableName SET stringa = REPLACE(stringa,' ','') --Now remove the spaces

FUNCTION

CREATE FUNCTION [dbo].[ProperCase]
(
@String VARCHAR(255)
)

RETURNS VARCHAR(255) AS

BEGIN

DECLARE @i INT
DECLARE @Char CHAR(1)
DECLARE @CorChar CHAR(1)
DECLARE @PrevAscii INT
DECLARE @PrevAscii2 INT
DECLARE @Ret VARCHAR(255)


/* Captalisation rules */

-- Capitalise first letter of each word
-- Capitalise next letter after special characters

-- eg joe o'bloggs-bloggs jr -> Joe O'Bloggs-Bloggs Jr

SET @Ret = ''
SET @i = 1
WHILE @i <= LEN(@String)

BEGIN
   SET @Char = SUBSTRING(@String, @i, 1)
   SET @CorChar = CASE  WHEN @i = 1                                 THEN UPPER(@Char)-- First letter
                    WHEN @PrevAscii = 32                        THEN UPPER(@Char)-- Follows Space
                    WHEN @PrevAscii = 39 AND @PrevAscii2 =  79  THEN UPPER(@Char)-- Follows O'
                    WHEN @PrevAscii = 45                        THEN UPPER(@Char)-- Follows Dash
                    WHEN @PrevAscii = 46                        THEN UPPER(@Char)-- Follows Fullstop
                    ELSE LOWER(@Char)
                 END
   SET @Ret = @Ret + @CorChar

   SET @i = @i + 1
   SET @PrevAscii2 = @PrevAscii
   SET @PrevAscii = ASCII(@CorChar)
END

--Now sort out capitalistaion for van, de, den, and der

SET @Ret = CASE WHEN @Ret LIKE 'Van %' THEN 'v' + SUBSTRING(@Ret,2,255) ELSE @Ret END
SET @Ret = CASE WHEN @Ret LIKE 'De %' THEN 'd' + SUBSTRING(@Ret,2,255) ELSE @Ret END
SET @Ret = CASE WHEN @Ret LIKE 'Der %' THEN 'd' + SUBSTRING(@Ret,2,255) ELSE @Ret END
SET @Ret = CASE WHEN @Ret LIKE 'Den %' THEN 'd' + SUBSTRING(@Ret,2,255) ELSE @Ret END

SET @Ret = REPLACE(@Ret,' De ',' de ')
SET @Ret = REPLACE(@Ret,' Der ',' der ')
SET @Ret = REPLACE(@Ret,' Den ',' den ')

RETURN @Ret

END
twoleggedhorse
  • 4,938
  • 4
  • 23
  • 38
  • I used Microsoft word to solve this problem. First Capitalized the string and then replace underscore (_) with blank(). My problem is solved. Thanks a lot for your help. – Dixit Chopra Nov 09 '12 at 12:15