Regular expression with termination specifies to bring 3 first characters

4

I'm trying to do a regular expression in Oracle with the following requirement:

  • Finish with a specific letter, if you have that letter in the middle do not search.
  • Return the first 3 digits of the string.

To solve the first problem I can use:

a\b

The "a" being the letter, \b to get a border position after the letter.

To solve the second problem, ie return the first 3 digits:

^(...){1,1}

Now how do I join the two conditions? Or the way I'm doing it would not work?

Examples, which end with the letter "a", need to return the first 3:

  

anh pro

    
asked by anonymous 15.03.2017 / 14:04

2 answers

3

I was thinking of a single expression, but I do not need to, because I need to filter all that ends with the letter I want, and show the first 3 characters, thus getting the script:

SELECT regexp_substr(t.string,'^(...){1,1}')
  FROM tabela t
 WHERE regexp_like(t.string,'A$')   

You have to change a\b to A$ , because Oracle does not recognize it. Tips and suggestions are always welcome.

I used the documentation to help me: Using Regular Expressions With Oracle Database

    
15.03.2017 / 14:27
1

The abstraction you have shown in your own answer is correct, but let's see what the REGEX would look like.

(\S{3})\S*?m\b

Explanation

I can divide this REGEX into 3 parts:

  • (\S{3}) - Capture 3 characters, other than spacing. see more .
  • \S*? - Capture infinite characters, other than spacing, but the minimum required. see more .
  • m\b - As you said yourself, delimit the border.

REGEX101

    
16.03.2017 / 12:46