表格中有以下字符串:
step1.cards.choice_step_1
step1.cards.choice_step_2
step2.cards.choice_step_1
step2.cards.choice_step_2
我会使用该模式替换所有字符串中的空字符:
^step([0-9]|[1-9][0-9]).cards.choice_step_1$
^step([0-9]|[1-9][0-9]).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 :(得分: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 |
+------------------------------------+------+------+-------+
注意包含所需数据的最后一个字段。请随意根据您的需要进行调整。