1

I have many strings that I need to tokenise - an example is shown below.

NAME:ALAN GREYINTACC:999ACCOUNT:12345678SORT:654321REF:ABC 7654321

I can use a combination of INSTR and SUBSTR to do this, but I was hoping that there might be an easier way to perform this using REGEXP_SUBSTR.

I've looked at anchors, but could only find references to start string ^ and end string $.

Is there such a thing as an 'in string' anchor? e.g. If I wanted the account number from this string, I want everything after ACCOUNT: and before SORT:

Any replies appreciated,

Many Thanks

2 Answers 2

2

Use REGEXP_REPLACE instead for this purpose. This can help you to capture groups(like \1) from the matching and use it in replacement. See below example:

SELECT
  REGEXP_REPLACE('NAME:ALAN GREYINTACC:999ACCOUNT:12345678SORT:654321REF:ABC 7654321',
                  '.*ACCOUNT:(.+?)SORT.*',
                  '\1') "SOME_VALUE"
FROM DUAL;
Sign up to request clarification or add additional context in comments.

1 Comment

I'd just worked this out by trying to solve the question. REGEXP_SUBSTR does not allow for capturing of matches, so REGEXP_REPLACE is the way to go. +1.
0

There's no 'in string' anchor per say: there are patterns that your regex can match. Even 'anchor' are really matches for the end and beginning of the line.

This regex, for example, will match ACCOUNT: followed by anything, followed by SORT: and store this 'anything' in the first capturing group (indicated in the regex with the parenthesis)

ACCOUNT:(.*?)SORT:
  • . is any character (except newline)
  • * is a quantifier, means 0 or more times
  • ? makes the quantifier lazy (will stop at the shortest match, useful if your data is ACCOUNT:123SORT:456SORT:789 and you only want to select 123, not 123SORT:456)

4 Comments

Thanks Robin. Unfortunately, due to the state of the data I'm being supplied with, there can be pretty much anything between ACCOUNT: and SORT: so I can't just look for numbers.
@user3463817: I see. I updated my answer accordingly, but I'm not familiar with oracle's regex and I don't know how to display the captured value. Good luck
Having played around with REGEXP_SUBSTR, I couldn't figure a way of actually using the capture. As Sabuj suggests, REGEXP_REPLACE is required for this.
Thank you all very much for answering this question. Much appreciated! REGEXP_REPLACE it is!!!!

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.