0

I need to extract some text from a string, but only where the text matches a string pattern. The string pattern will consist of...

2 numbers, a forward slash and 6 numbers e.g. 12/123456

or

2 numbers, a forward slash, 6 numbers, a hyphen and 2 numbers e.g. 12/123456-12

I know how to use INSTR to find a specific string. Is it possible to find a string that matches a specific pattern?

4
  • Which string pattern do you need to match in order for the data to qualify for extract? Commented Sep 22, 2017 at 16:18
  • 2 numbers, a forward slash and 6 numbers e.g. 12/123456 Commented Sep 22, 2017 at 16:20
  • This could be contained in a string with a mixture of alpha-numerics, but will have a space either side Commented Sep 22, 2017 at 16:21
  • You said "I need to extract some text from a string". What text do you need to extract? The same substring (nine characters) you need to find as a pattern? Or do you need to return the FULL string, but only if it matches the pattern? Commented Sep 22, 2017 at 18:52

3 Answers 3

1

You'll need to use regexp_like to filter the results and regexp_substr to get the substring.

Here is roughly what it should look like:

select id, myValue, regexp_substr(myValue, '[0-9]{2}/[0-9]{6}') as myRegExMatch
from Foo
where regexp_like(myValue,'^([a-zA-Z0-9 ])*[0-9]{2}/[0-9]{6}([a-zA-Z0-9 ])*$')

with a link to a SQLFiddle that you can see in action and adjust to your taste.

The regexp_like provided in the sample above takes into consideration the alphanumerics and whitespace characters that may bound the number pattern.

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

2 Comments

There is no need to escape the forward slash in regular expressions. Also, why are you requiring all the OTHER characters to be alphanumeric? There was nothing about that in the OP's question; for example, if there are dashes (-) elsewhere in the input string, this solution will say there is no match.
You are correct. I have updated the solution to reflect that you don't need to escape the slash. However, escaping the slash did no harm. In terms of the the "other characters" (if you are referring to the ([a-zA-Z0-9 ])* in the solution) this was done because the OP indicated "2 numbers, a forward slash and 6 numbers" but that "[It] could be contained in a string with a mixture of alpha-numerics, but will have a space either side"
0

Use regexp_like.

where regexp_like(col_name,'\s[0-9]{2}\/[0-9]{6}(-[0-9]{2})?\s')
  • \s matches a space. Include them at the start and end of pattern.
  • [0-9]{2}\/[0-9]{6} matches 2 numerics, a forward slash and 6 numerics
  • (-[0-9]{2})? is optional for a hyphen and 2 numerics following the previous pattern.

Comments

0
regexp_like(col_name,'^\d{2}/\d{6}($|-\d{2}$)')

or

regexp_like(col_name,'^\d{2}/\d{6}(-\d{2})?$')

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.