简体   繁体   中英

How can I search for occurences in a string with Oracle REGEXP_INSTR

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 .

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.

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

试试这个。放锚。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM