2

I'm trying to do the following operation. I have many lines of data that needs to be edited to delete a part of a string. This string contains a pattern B0ExxB+, where 'x' could be any character or number.

  • Input example (string) : '1SX8+B0DSUB+B0E0LB+B0FMAB+B0G0KB'
  • Desired output : '1SX8+B0DSUB+B0FMAB+B0G0KB'

I use the REGEXP_REPLACE operation. have two strategies to accomplish the operation:

  1. Known the pattern B0E apply more regex operators to pick also the 4 following characters after the pattern, and then replace them by nothing.

    • Main idea.

    update rx3qtxin xn set cin.cin_value = REGEXP_REPLACE (cin.cin_value, '\SB0E', '') where cin.id = 500228;

That deletes the pattern, OK. I've tried with adding as many ? after the pattern but it delete any other part of the string.

  1. Known left pattern (B0E) and right pattern (B+) delete where both patterns are matched and the content between them.
    • No idea how to apply that idea in regex.*
6
  • Please rephrase your question by showing clear input with expected output. Commented Mar 3, 2020 at 13:54
  • Maybe REGEXP_REPLACE(col, 'B0E[[:alnum:]]{2}B[+]', '')? Commented Mar 3, 2020 at 13:56
  • 1
    What's with the \S in the regular expression in your attempt? There is no mention of it in the problem description. Commented Mar 3, 2020 at 14:12
  • Jorge, do you mean you want to avoid matching BBExxB+? Try REGEXP_REPLACE('1SX8+B0DSUB+B0E0LB+B0FMAB+B0G0KB', '(^|\+)B0E[[:alnum:]]{2}B\+(B0E[[:alnum:]]{2}B\+)*', '\1') then (it also matches consecutive occurrences). Commented Mar 3, 2020 at 14:33
  • After your edit, does it mean my first suggestion worked for you? Commented Mar 3, 2020 at 14:58

1 Answer 1

4

The question mark is a placeholder for a single character in Unix ("wildcard character"). In regular expressions, for the same task we use the dot (period). The question mark (in regular expression) just makes the subexpression it applies to optional. It won't help you with your task.

You need something like

regexp_replace(col, 'B0E..B\+') 

Note that the "plus sign" must be escaped, since unescaped it is a metacharacter.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect. I thought i need a regex operator before the pattern.

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.