Regular expression with specific ending bring 3 first characters

Asked

Viewed 972 times

5

I am trying to make a regular expression on Oracle with the following requirement:

  • End with a specific letter if you have this letter in the middle not searching.
  • Return the first 3 digits of the string.

To solve the first problem I can use:

a\b

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

To solve the second problem, that is, return the first 3 digits:

^(...){1,1}

Now how do I put the two conditions together? Or the way I’m doing wouldn’t work?

Examples, which end with the letter "a," and I need the first three to return:

anhAnguera
probluff
perGunta

2 answers

4


I was thinking of a single expression, but I don’t need it, because I need to filter everyone that ends with the letter I want, and show the first 3 characters, so the script:

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

Have to change the a\b for A$, because Oracle does not recognize. Tips and suggestions are always welcome.

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

  • 1

    OBS.: {1,1} is doing nothing, for default it will capture 1 time, and as its repetition is limited to minimum 1 maximum 1, the effect is the same staying only ^(...) REGEX101

  • Thank you William

1

The abstraction that you showed in your own answer is correct, but let’s see how REGEX would be completely.

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

Explanation

I can divide this REGEX into 3 parts :

  • (\S{3}) - Captures 3 characters other than spacing. see more.
  • \S*? - Captures infinite characters, which are not spacing, but the minimum required. see more.
  • m\b - As you said yourself, it borders.

REGEX101

  • Thank you William, the expression in regex101 shows on the right side the first group and the full match, no rule returns the full match.

  • @David the links are with default values, would have to save the to lynch, the regex101 just tighten ctrl+s. On the question of not showing the group, it can be the tool.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.