REGEXP 用于字符串格式化,以空格分隔字符和数字进行分组。

huangapple go评论68阅读模式
英文:

REGEXP to use for String formatting to group characters and numbers separated with spaces

问题

尝试使用正则表达式函数来格式化字符串。例如:

REGEXP_REPLACE(input_String, '([0-9]+|[a-zA-Z]+)', ' ')
英文:

Hello I am trying to format a given string so that it groups numbers and characters and puts spaces ' ' in between.
For example the given string: 01ABC23fg98
should give the output: 01 ABC 23 fg 98

Any suggestions?

I tried REGEXP_REPLACE(input_String , ' ', '')
which does the opposite (it removes spaces in a given string)

答案1

得分: 2

这可以通过使用 REGEXP_REPLACE() 和捕获组来实现:

SELECT input_String,
       TRIM(REGEXP_REPLACE(input_String, '([A-Za-z]+)', '  ')) AS output_String
FROM yourTable;
英文:

This can work if we use REGEXP_REPLACE() with a capture group:

<!-- language: sql -->

SELECT input_String,
       TRIM(REGEXP_REPLACE(input_String, &#39;([A-Za-z]+)&#39;, &#39;  &#39;)) AS output_String
FROM yourTable;

<h2>Demo</h2>

This regex replacement replaces all letter word substrings with the same substring surrounded by spaces on both sides. Appreciate that this effectively separates letter word substrings from number substrings. The outer call to TRIM() is necessary because should a letter word occur at the start or end of the input string, the replacement would result in a leading or trailing space.

答案2

得分: 0

当然,但是你没有告诉我你想要用哪种编程语言,但无论如何,我会用Python完成,看这里:

import re

input_string = '01ABC23fg98'
output_string = re.sub(r'(\d+|\D+)', r' ', input_string).strip()

print(output_string)
英文:

Sure,but you didnt tell which programing language you want this but anyways i will do this on python here we go :

import re

input_string = &#39;01ABC23fg98&#39;
output_string = re.sub(r&#39;(\d+|\D+)&#39;, r&#39; &#39;, input_string).strip()

print(output_string)

the output must look like this : 01 ABC 23 fg 98

huangapple
  • 本文由 发表于 2023年3月3日 21:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定