2015-12-14 111 views
0

我在表中具有下列字符串:MySQL正用空字符替换字符

^step([0-9]|[1-9][0-9]).cards.choice_step_1$ 
^step([0-9]|[1-9][0-9]).cards.choice_step_2$ 

它可能:

step1.cards.choice_step_1 
step1.cards.choice_step_2 

step2.cards.choice_step_1 
step2.cards.choice_step_2 

我会用空字符使用该模式取代所有这些字符串在MySQL中这样做?

最后我会在结果:

1 
1 
2 
2 

编辑

select regex_replace('[^0-9]','','step1.cards.choice_step_1'); 

还给我

11 

请在正确的方式帮助以下数据:

sometext.step1 
sometext.step1.cards.choice_step_1 
sometext.step1.cards.choice_step_2 
sometext.step1.desire 

sometext.step2 
sometext.step2.cards.choice_step_1 
sometext.step2.cards.choice_step_2 
sometext.step2.desire 

得到以下结果:

step1 
step1 
step1 
step1 

step2 
step2 
step2 
step2 
+0

可能重复[如何在MySQL中执行正则表达式替换?](http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql) – miken32

回答

1

你可以尝试这样的事:

create table test (field1 varchar(100)); 
select * from test; 
+------------------------------------+ 
| field1        | 
+------------------------------------+ 
| sometext.step1      | 
| sometext.step1.cards.choice_step_1 | 
| sometext.step1.cards.choice_step_2 | 
| sometext.step1.desire    | 
| sometext.step2      | 
| sometext.step2.cards.choice_step_1 | 
| sometext.step2.cards.choice_step_2 | 
| sometext.step2.desire    | 
| step1        | 
+------------------------------------+ 

查询:

select 
    field1, 

    -- find position of step 
    position('step' in field1) as step, 

    -- find position of dot AFTER step is found 
    position('.' in 
      substr(field1, position('step' in field1), length(field1))) 
      + position('step' in field1) as dot, 

    -- if position of 'step' --and-- position of 'step' + position of 'dot' is 1 
    -- that means: step is at position 1 and dot is not found, display field as is 
    -- if position of 'step' --and-- position of 'step' + position of 'dot' are equal 
    -- that means: dot is not found. Grab everything from step onwards 
    -- if position of 'step' --and-- position of 'step' + position of 'dot' are **NOT** equal 
    -- grab everything from where step was found thruogh just before dot was found 
    case 
    when position('step' in field1) = 1 
     and 
     position('.' in substr(field1, position('step' in field1), length(field1))) 
     + position('step' in field1) = 1 
     then 
      field1 
    when position('step' in field1) = 
     position('.' in substr(field1, position('step' in field1), length(field1))) 
     + position('step' in field1) 
     then 
      substr(field1, position('step' in field1), length(field1)) 
    else 
     substr(field1, 
      position('step' in field1), 
      position('.' in substr(field1, position('step' in field1), length(field1)))-1 
     ) 
    end as ans 

from test; 

结果:

+------------------------------------+------+------+-------+ 
| field1        | step | dot | ans | 
+------------------------------------+------+------+-------+ 
| sometext.step1      | 10 | 10 | step1 | 
| sometext.step1.cards.choice_step_1 | 10 | 16 | step1 | 
| sometext.step1.cards.choice_step_2 | 10 | 16 | step1 | 
| sometext.step1.desire    | 10 | 16 | step1 | 
| sometext.step2      | 10 | 10 | step2 | 
| sometext.step2.cards.choice_step_1 | 10 | 16 | step2 | 
| sometext.step2.cards.choice_step_2 | 10 | 16 | step2 | 
| sometext.step2.desire    | 10 | 16 | step2 | 
| step1        | 1 | 1 | step1 | 
+------------------------------------+------+------+-------+ 

注意具有您所需数据的最后一个字段。请随时根据您的需求调整它。

+0

看起来不错谢谢 –