0

I have a string like A12345678B

I want to be able to check if a string starts with a character, is followed by 8 digits and ends with a character.

We are trying:

SELECT REGEXP_INSTR('A12345678B', '[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{10}',1,1,1,'i') from DUAL

This returns :

11

We want to be able to determine that if a string does NOT start and end with a character and is not followed by 8 digits after the first character ( see sample string above ), THEN this is not the string that we are looking for .

3 Answers 3

4

string starts with a character

^[[:alpha:]]

is followed by 8 digits

[[:digit:]]{8}

ends with a character

[[:alpha:]]$

So the complete regex would be,

^[[:alpha:]][[:digit:]]{8}[[:alpha:]]$

This [[:alpha:]]{10} in your regex assumes that there must be exactly 10 alphabets present after to the 8 digit number.

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

Comments

1
^[a-zA-Z][0-9]{8}[a-zA-Z]$

Try this.Put the anchors.

Comments

1

Try using regexp_like if you want a full string match. For a match, use:

WHERE REGEXP_LIKE('A12345678B', '^[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{1}$') 

For a non-match use:

WHERE NOT REGEXP_LIKE('A12345678B', '^[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{1}$') 

Note: this assumes that you want to filter the results (which is what your question implies, not put a flag into the select clause.

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.