1

I need to get text between repeating asterisks in postgresql. String can be either like

"120 B-4, Gl-12 ***Test data****" Some other text" (with text between the asterisks)

or

*****(withoout text)

There can be two or more asterisks When there is text between asterisks result should be the text between the strings but when there is no text it should return empty string. There can be two or more asterisks and there might not be text between asterisks in that case result should be empty string. I was trying to achieve this by using regexp_split_to_array

regexp_split_to_array(o."Posts" , '([\*]{2,})'))[2]).

It works fine when there is text between the strings but when there is no data between the strings it fails.

Edit I think I am doing it the wrong way I need an expression which can match anything between asterisks other than asterisk

4
  • Your first example has only one asterisk making it hard to define 'between' in that context. Commented Jun 3, 2021 at 20:07
  • Read up on positive lookahead and positive lookbehind Commented Jun 3, 2021 at 20:26
  • @ChrisMaurer I forgot to escape asterisks. I think I need an expression which can match anything between asterisks other than asterisk. Commented Jun 3, 2021 at 20:26
  • ...and how to interpret your multiple *? I would try to return a 6 element array all blank except for the third element (return values for EACH consecutive pair of asterisks. Commented Jun 3, 2021 at 20:34

1 Answer 1

1

The following might work for you:

SELECT regexp_matches(x, '[*]+([^*]+)[*]+', 'g');

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  [*]+                     any character of: '*' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^*]+                    any character except: '*' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [*]+                     any character of: '*' (1 or more times
                           (matching the most amount possible))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.