英文:
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, '([A-Za-z]+)', ' ')) AS output_String
FROM yourTable;
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 = '01ABC23fg98'
output_string = re.sub(r'(\d+|\D+)', r' ', input_string).strip()
print(output_string)
the output must look like this : 01 ABC 23 fg 98
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论