简体   繁体   English

PostgreSQL正则表达式替换文本字段

[英]Postgresql regex replace text field

Hi guys I have question about regex, can you help me extract date from text like: 嗨,我对正则表达式有疑问,您能帮我从类似这样的文本中提取日期吗:

Start   20130918 14:35:00

I wan extract 20130918 only from text. 我只想从文字中提取20130918。 I've tried something like this: 我已经尝试过这样的事情:

regexreplace(Start('\s+\w{5}\d{8}\s',''))

对于此类问题,最好使用子字符串而不是regex_replace:

select substring('Start   20130918 14:35:00' from '[0-9]{8}')

You can use the following expression for matching the line: 您可以使用以下表达式来匹配该行:

Start\s+([0-9]{8})\s.*

and then a replacement string: \\1 . 然后是替换字符串: \\1

NOTE: You might need to double escape each backslash if you are passing the expression as a string, so you might need to use: 注意:如果将表达式作为字符串传递,则可能需要对每个反斜杠加倍转义,因此可能需要使用:

Start\\s+([0-9]{8})\\s.*

EDIT: You can use the following statement: 编辑:您可以使用以下语句:

SELECT regexp_replace('Start 20130918 14:35:00','Start\s+([0-9]{8})\s.*','\1')

and here is an SQL Fiddle Demo 这是一个SQL Fiddle演示

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

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