简体   繁体   English

mySQL regex_replace 从混合输入中删除所有开头的数字

[英]mySQL regex_replace remove all beginning numbers from mixed input

I need a regex pattern to remove all numeric input from the beginning of a field that stops at the first alphabetic character and leaves any remaining numbers that follow alphabetic characters.我需要一个正则表达式模式来删除字段开头的所有数字输入,该字段在第一个字母字符处停止,并留下字母字符后面的任何剩余数字。

Here is the simplest test case.这是最简单的测试用例。

CREATE TABLE tempdemo (tdfields varchar(10));
INSERT INTO tempdemo (tdfields) VALUES ('1'), ('11'), ('111'), ('a'), ('ab'), ('abc'), ('1a'), ('a1');

SELECT REGEXP_REPLACE(tdfields, '^[0-9]*$', '') FROM tempdemo;

From the code, I would like the following output: :blank:, :blank:, :blank:, a, ab, abc, a, a1从代码中,我想要以下 output: :blank:, :blank:, :blank:, a, ab, abc, a, a1

Right now, the regex pattern leaves '1a' instead of 'a' which is undesireable.现在,正则表达式模式留下 '1a' 而不是 'a',这是不可取的。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

You can use您可以使用

CREATE TABLE tempdemo (tdfields varchar(10));
INSERT INTO tempdemo (tdfields) VALUES ('1'), ('11'), ('111'), ('a'), ('ab'), ('abc'), ('1a'), ('a1');

SELECT REGEXP_REPLACE(tdfields, '^[0-9]+', '') FROM tempdemo;

It will remove one or more digits from the start of the string .它将从字符串的开头删除一个或多个数字

More details :更多详情

  • ^ - start of string ^ - 字符串的开头
  • [0-9]+ - one or more digits. [0-9]+ - 一位或多位数字。

$ matches the start of string position. $匹配字符串 position 的开头。

Note that ^[0-9]* is not a good pattern here since it matches any string, and + variation will only match when necessary, when there are digits at the start of the string.请注意,这里的^[0-9]*不是一个好的模式,因为它匹配任何字符串,并且+变体仅在必要时匹配,当字符串开头有数字时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM