正则表达式 - 更改为驼峰大小写/删除下划线

时间:2012-11-09 11:06:23

标签: regex sql-server-2008-r2 notepad++ ssms

我想将记事本/ Sql Server管理工作室中的所有字符串转换为驼峰大小写并删除所有下划线。

E.g:

string a = REFERENCE_DATA_ID
String b = ReferenceDataId

我想删除字符串a中的下划线并隐藏到驼峰的情况下,如字符串b所示。请帮帮我。

2 个答案:

答案 0 :(得分:2)

您可以在记事本++中执行此操作:

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

唯一的问题是,您需要知道得分不足的最长时间以及字符串的结束方式。 在上面的例子中,我将php文件名替换为camelCase,知道得分不能超过3次,少则没问题。

答案 1 :(得分:1)

假设您的数据已经在表中,是否必须使用reg ex?为什么不创建标量函数来执行此操作并按照以下步骤操作:

  1. UPDATE tableName SET stringa = REPLACE(stringa,'_','') - 用空格替换下划线
  2. UPDATE tableName SET stringa = dbo.ProperCase(stringa) - 使用我在下面创建的函数更新字段
  3. UPDATE tableName SET stringa = REPLACE(stringa,'','') - 现在删除空格
  4. 功能

    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