简体   繁体   中英

Find simple position of regexp match in string in PostgreSQL

I am trying to find the first position of regexp in the string (the exact position id) to be able to delete it, but I can't find such a solution in PostgreSQL.

My query eats separators and combines strings and tries to cut only values between separators. For example, when I have a string value in a column such as:

57080*570801*157080*5708011
670811*67081*670810*670815

I tried to use:

UPDATE tab 
set str = (REGEXP_REPLACE(str, '^57080*', '')
WHERE myColumn=159880;

or

UPDATE tab 
set str = (REGEXP_REPLACE(str, '*57080*', '')
WHERE myColumn=159880;

or

UPDATE tab 
set str = (REGEXP_REPLACE(str, '*57080$', '')
WHERE myColumn=159880;    

However, this doesn't help me because after using (REGEXP_REPLACE (str, '* 57080 *', '') I have a stuck id (eg 57080570801 ). I need to find my regexp position somehow to be able to cut only the id. After that I can always use REPLACE (string, '**', '*') which will cut out double characters or

SET str = CASE
WHEN str LIKE '*%' THEN RIGHT (str, LENGTH (str) -1)
WHEN str LIKE '% *' THEN LEFT (str, LENGTH (str) -1)

will cut the start / end characters ( * ). Does anyone have any idea how to easily find positions of such a regexp.

Use the “beginning of word” and “end of word” markers:

regexp_replace(str, '\m12345\M', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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