using regexp_substr

Hello

I need to extract the first five characters digital of the specified column. I'm able to reach the requirement using substr and definition of functions.

I learned that the use of regular expressions we can achieve in a simple way. Please find below the test data.

with t as (select '123456suri' double empid

Union all select 'suri436789' of the double

Union all select '521521suri' of the double

Union select all 'suri785643abc' of the double)

select * from t ;

Desired output:

EmpID

---------

12345

52152

Please help me how to extract the results required using REGEXP_SUBSTR.

I tried with query below, but not able to restrict the result to only five digits.

Select double regexp_substr('3456789suri123','[0-9]+',1,1)

Thank you

Suri ;-)

Post edited by: Suri - added query

Good first try

\d is a shortcut for [0-9]

What happens when you try to \d\d?

How about \d\d\d?

You should be able to guess the rest of there

You can also use {n} means "repeat exactly n times.

Tags: Database

Similar Questions

  • How to search for a string with various occurrences using regexp_substr

    Hi all.

    My cenario's

    '.... 456re0, 50kg 400, 500rfabs43qre30, 25kg 150, 354rf658... »


    It is possible, using regexp_substr or another way to get the values, 0,50 and 400 500 and 30.25 150 354?

    I am using [^ d] + [$kg] and the string comes, but only the first occurrence...

    TKS.

    claudioaragao wrote:
    I need the results of...

    with sample_table as (
                          select '....456re0,50kg400,500rfabs43qre30,25kg150,354rf658....' str from dual
                         )
    select  regexp_substr(str,'re.*?rf',1,column_value) sub_str
      from  sample_table,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by regexp_substr(str,'re.*?rf',1,level) is not null
                               )
                       as sys.OdciNumberList
                      )
                )
    / 
    
    SUB_STR
    -------------------
    re0,50kg400,500rf
    re30,25kg150,354rf
    
    SQL> 
    

    SY.

  • Split a string using regexp_substr with consecutive delimiter

    I split a string with tubes as a delimiter. A string like this for example:

    "THIS |" EAST | ONE | EXAMPLE '

    If I do something like this:

    SELECT REGEXP_SUBSTR('THIS|IS|AN|EXAMPLE', '[^|]+', 1, 4) FROM DUAL
    

    I would get the word EXAMPLE

    But if the string is like this:

    "THIS |" EAST | ONE | EXAMPLE '

    With the above query, I always get EXAMPLE, but the word should be in the next position (5) because after IS, there should be an empty element

    Is it possible to change the regular expression to also get the empty element?

    Thanks in advance

    Well, the way I understand it, you get this behavior, because there is 'nothing' between the delimiters. so there is a field, so it does not display it don't think the regular expression.

    Try it with a space between the delimiters - works fine.

    So, with this in mind, the simplest solution might be something like that?

    regexp_substr (replace(c,'||','| |'), "[^ |]") +' 1, 4)

    [edited to avoid having a partial answer marked "correct"]

    As mentioned below by a few others, there are other solutions using regular expressions.

    The solution above does not work if more than 2 consecutive fields are empty, or if the first or last is empty.

    A simple tweak to the logic would help with this:

    RTrim (regexp_substr (replace (c,'|))) ',' |'), ' [^ |] +', 1, level))

    However, even if it has still some limitations compared to the solution of the full regular expression mentioned by Frank, below.

    [/ Edit]

  • Split the cell enclosed in several lines using REGEXP_SUBSTR - Performance?

    Hello

    I have a table that has about 20,000 lines. There is a column called key word which has values like below:

    File_id Keyword
    1SMITH; ALLEN; WARD; JONES; BRADY
    2S & P500; TOPIX
    3SMALL; LARGE; MEDIUM

    I want to display the data like this:

    File_id KEYWORD
    1SMITH
    1ALLEN
    1WARD
    1BRADY
    2S & P500
    2TOPIX
    etc.

    I use this application and it works:

    SELECT STG. FILE_ID, REGEXP_SUBSTR (STG. KEYWORD ' [^;] +', 1, LEVEL) AS A KEYWORD OF STG STG_TABLE

    CONNECT REGEXP_SUBSTR (STG. KEYWORD ' [^;] +', 1, LEVEL) IS NOT NULL

    But its so slow its unusable.

    Is there a faster way to return this output?

    Other info:

    KEY Word is varchar2 (4000), but rarely more than 100 bytes are used

    Oracle 11g 2

    Thank you!

    Wrong approach, there are too many lines that are generated

    Apart from replacing regexp with substr/instr, try the first slot

    SELECT

    STG. FILE_ID

    REGEXP_SUBSTR (STG. KEYWORD ' [^;] +', 1, LEVEL) AS KEYWORD

    OF STG STG_TABLE

    CONNECT

    level<= regexp_count(stg.keyword,';')="" +="">

    and prior STG. FILE_ID = STG. FILE_ID

    and prior sys_guid() is not null

  • Find ' [' and get the substring entered using regexp_substr()]

    Hello

    Select regexp_substr ("what a great day for (I YOU [ME] and HIM ',' [: ALNUM:] * [^ \ ()] *') double;")
    As a result:
    SQL > what a great day for

    [but why this model does not work for ']' when I replace ' ([' with ']'?)
    Select regexp_substr ("what a great day for (I YOU [ME] and HIM ',' [: ALNUM:] * [^ \]] *') double;")
    As a result:
    SQL > W

    ' {[I need to get a substring before or ' (',' [',' {', '}])', ']', or '}' in a string to give, this model works for '(',') ', ' {', '}' but not ' [' or ']'
    What should be the boss? If ' [: ALNUM:] * [^ \ ()] *' is not correct?

    Can anyone help?

    Thank you
    Fiona

    Hi, Fiona,

    Have you seen this part of my last post?

    Frank Kulash wrote:
    ... If you want to use a literal hook closing ('] ') then it must be the first thing in the list attached hook (not counting the ^, if you use it, you are)

    In a regular expression, if you want to say: "the set of all characters * except * ' {' and '}'", so it does not matter if you say

    [^{}]
    

    or

    [^}{]
    

    Most of the people (you and me included) seem to like the first better way, but the results are the same. It deosn to matter if the ' {' is listed before the '}', or if they are in reverse order. The Analyzer don't think you want to say something else anyway.

    However, the parser will treat this expression

    [^[]]
    

    different than

    [^][]
    

    The first:

    [^[]]
    

    is interpreted as

    '['          ||     -- Any character from this set:
       '^'          ||     --     all characters except
       '['          ||     --     left-bracket
    ']'          ||     -- (end of set description)
    ']'               -- followed immediately by a right-bracket
    

    You want to support the right to part of the whole. The only way that the parser will know that the right-braket marks not the end of the game is to put the right first overall support, in other words, it must be the first character inside the pair of brackets that delimited the game, immediately after the ' ^' character, like this:

    '['          ||     -- Any character from this set:
       '^'          ||     --     all characters except
       ']'          ||     --     right-bracket or
       '['          ||     --     left-bracket
    ']'               -- (end of set description)
    

    or, as you're more likely to write it:

    select      'What a good day for (YOU [ME] and {HIM}) and others'     AS orig_txt
    ,     REGEXP_REPLACE ( 'What a good day for (YOU [ME] and {HIM}) and others'
                     , '\[[^][]*\]'
                     )                         AS new_txt
    from      dual
    ;
    

    Output:

    ORIG_TXT
    ---------------------------------------------------
    NEW_TXT
    -----------------------------------------------
    What a good day for (YOU [ME] and {HIM}) and others
    What a good day for (YOU  and {HIM}) and others
    
  • REGEXP_SUBSTR - how to use case-sensitive

    I use REGEXP_SUBSTR with case-insensitive option

    while I use as below

    Select REGEXP_SUBSTR ("uk123 research", "UK", "i") double column;

    It displays error: ORA-01722: invalid number

    Help, please...

    Oracle 11.2.0.1.0

    You provide all the required parameters. See the http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions138.htm#SQLRF06303 documentation

    Here's the correct query:

    select REGEXP_SUBSTR('search for uk123', 'UK',1,1,'i' )  col_name from dual;
    
  • Regexp_substr

    How to get the output below using regexp_substr

    with t as(
    select 'APP_CONTACT_INFO' s from dual union
    select 'APP_CONTACT' from dual union
    select 'APP_CONTACT_TYPE_100200' from dual
    )
    
    
    
    
    select 
    s,
    REGEXP_SUBSTR( S,'_[A-Z]{3}',1) 
    
    
    
    
    from t;
    
    
    
    
    
    
    
    
    
    CON
    CONINF
    CONTYP100
    
    

    How to repeat _ [A - Z] {3} for each underscore match and concatenate it

    Select regexp_replace ('myTWst', '. *([A-Z]+). *', '\1') twice;

    only gives you a result of 'W' because the '. '. ' * ' in the expression is "greedy".  This means that it will match both the string he can before the rest of the evaluation of the model.

    So the first '. '. "*" matches "myT", then the "([A-Z] +)" ends up with only the W to match against and the final "." "*" corresponds to the "m".

    If instead you did:

    SQL > select regexp_replace ('myTWst', '. *?) () [A-Z] +). (* $', '\1') twice;

    RE
    --
    TW

    the '? 'after the first'. '. "*" he is not greedy, so it takes what is necessary to allow the rest of the pattern to match, wherever it is just 'my' instead of 'myT '.

    the "$" at the end indicates the last '. '. "*" to match all characters up to the end of the string, but even if it is greedy in itself, it is preceded by the "([A-Z] +) ' which itself is greedy and capitals as far as possible."

    If in fact told us the "([A-Z] +)" for do not be greedy by putting a "?" after the "+" then it takes as much as necessary to then leave the rest of the pattern (in our case the greedy"." (' $* ') to match as much as possible.

    SQL > select regexp_replace ('myTWst', '. *?) () [A-Z] + ?). (* $', '\1') twice;

    R
    -
    T

    So, now the '. '. "* $" is being greedy and account as much as possible while leaving enough for non greedy "([A-Z] +?)" for a match, which is just 1 uppercase letter. "  As you can see, the result is just the first letter capitalized.

  • How to extract the value of a tag XML using regular Expressions

    We get a response XML from a WEB SERVICE.

    I convert it to VARCHAR2.

    Now, I want to get the real answer that is inside the tag of the response.

    I tried this:

    DECLARE

    V_1 VARCHAR2 (30000): = ' < soap: Body > < ns:ProcessArgusFeedsResponse xmlns:ns = "urn: PegaRULES:SOAP:ArgusToPegaFeeds:Services" > ' |

    '< response > good < / answer >< / ns:ProcessArgusFeedsResponse > < / soap: Body > ';

    v_response VARCHAR2 (100);

    BEGIN

    DBMS_OUTPUT. PUT_LINE (V_1);

    v_response: = REGEXP_SUBSTR (v_1, ' / < >(.+?) < \/Response > answer /');

    dbms_output.put_line (v_response);

    END;

    It does not work.

    Any help would be greatly appreicated.

    Hello

    user12240205 wrote:

    We get a response XML from a WEB SERVICE.

    I convert it to VARCHAR2.

    Why?  XML has its own native ways of analysis; Why not use them?

    If you use regular expressions, then REGEXP_REPLACE, as shown above, is a good option in Oracle 10, but starting in Oracle 11.1, you can use REGEXP_SUBSTR like this:

    REGEXP_SUBSTR (v_1

    , '(.+?)'

    1

    1

    NULL

    1

    )

    The 6th argument is like a backreference; "He tells REGEXP_SUBSTR did not return to the entire organization, but only the part inside the 1st left '(' et correspondant à sa droite).

    The '?' to make it non-greedy is necessary only if v_1 can contain more than one response.

    Now, I want to get the real answer that is inside the tag of the response.

    I tried this:

    DECLARE

    V_1 VARCHAR2 (30000): = "" |

    'Good';

    v_response VARCHAR2 (100);

    BEGIN

    DBMS_OUTPUT. PUT_LINE (V_1);

    v_response: = REGEXP_SUBSTR (v_1, ' /(. +?)) <\ esponse="">/');

    dbms_output.put_line (v_response);

    END;

    It does not work.

    That's because it's looking for a slash ("/") before the '' tag and another after the ' tag.

    The backslash ("\") is not necessary here, but it is not nothing wrong.

  • REGEXP_SUBSTR returns the second group

    (occuranHi! Need help. I'm trying to use regexp_substr to get the channel number. But Oracle returns me NULL when I choose the second game.

    INPUT STRING:

    Line 255 #: 05:59:51.050639000: startup process filling 188234978

    When I choose the first (default) game - it returns:

    start the process of filling 188234978

    SQL:

    Select REGEXP_SUBSTR (' #255 line: 05:59:51.050639000: start filling out the process 188234978', '(start process fill) (\w+)',1,2( )

    de double


    Why?


    Database Oracle 11.2.0.4.0 x 64

    In my opinion, there are two groups:

    1 # start process filling 188234978

    2 # 188234978

    Yes, there are two groups, but that one game.

    Do not confuse the number of occurrence of a pattern for the number of a corresponding model group.

    It looks more like what you are looking for:

    select regexp_substr(
             'Line #255: 05:59:51.050639000 : start process fill 188234978'
           , 'start process fill (\d+)'
           , 1
           , 1     --< first occurrence of the pattern
           , null
           , 1     --< there you can choose the group in the matching pattern
           )
    from dual ;
    

    However, there are probably more effcient ways of doing, without regex.

  • Regexp_substr help


    String

    Waiting

    -Msg_erreur - process started with the parameters - Source schema:-MyPersonalSchema, target schema:-SandBoxTarget, Mode:-the TWO restart Id:-1453BOTH

    In above the string column grid I have string and I expect TWO to extract the string.

    So far, I have tried is underneath SQL, I know regular expression are powerful and strong enough to solve/snippet string using regexp_substr himself without the help of another function.

    However, I tried below, but being a newbie I came to this day only.

    SQL> with t (str) as (select '-Error_MSG- Procedure started with parameters - Source Schema:-MyPersonalSchema, Target Schema:-SandBoxTarget, Mode:-BOTH, Restart Id:-1453'
      2                            from dual)
      3  select regexp_substr(str,'([^:-]*)([^,]+)',1,3,'i',2) either_i_use_this
      4             ,regexp_substr(str,'([^:-]+)',1,6,'i')  How_to_remove_after_comma
      5    from t;
    
    
    EITHER_I_USE_THIS              HOW_TO_REMOVE_AFTER_COMMA
    ------------------------------ ------------------------------
    :-BOTH                         BOTH, Restart Id
    
    
    SQL>
    

    Oh Oops, I forgot to put the sign less in front BOTH in option

    where d)

    Select ' - msg_erreur - process started with the parameters - Source schema:-MyPersonalSchema, target schema:-SandBoxTarget, Mode: the TWO restart Id:-1453' double txt)

    Select txt, regexp_substr (txt, '. * (:(-) MODE?) (. *)) (en). (*', 1, 1, 'i', 3) d;

    That's why I meant 7 characters...

    as far as I know, only the OP can answer if the negative will always be there

  • RegExp_. Using substr

    Hi - I need to extract the string after the last ' / ' and everything before the last ' / '. Possible to do using substr and instr but I would like to know how this can be done using regexp_substr.

    for example: / Merchants/Retail/ShopBrand/Home/Holidays/Bed

    My requirement is to extract 'Reads', which is after the last ' / ' and '' / Merchants/Retail/ShopBrand/Home/Holidays which is before the last ' / '.

    Help, please

    Could you give the explanation as well to '. * /', '' ?

    We receive all the strings until the last ' / ' and substituting null

    And my second (not a Cleaver) option would be:

    SELECT REGEXP_REPLACE(REGEXP_SUBSTR('/Merchants/Retail/ShopBrand/Home/Holidays/Bed', '.*/'), '.$', '') FIRST_REQUIREMENT FROM DUAL;
    

    Here, we remove the last character. Take a look at the documentation to go further: http://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS1003

    Kind regards.

  • regexp_substr: retrieve a block of text between delimiters

    Hello

    regexp_substr is very powerful, but not so easy to understand. I'll try to do my best, but I need your help.

    My question is this: I have a multiline text, stored in a database field. The text is structured as a "windows.ini" file: there are sections in square brackets and the lines of text in the section.

    I want to retrieve all of the text in a given section, and I think it's possible using regexp_substr.

    Here is the text of the field (in bold the part I want back):

    ---

    [ARTICLE 1]

    AAA = 123

    BBB = 456

    CCC = 789

    [ARTICLE 2]

    DDD = 987

    EEE = 654

    ---

    In other words, is there a way to retrieve the text between [ARTICLE 1] and [ARTICLE 2]?

    Thank you in advance.

    Concerning

    Select

    regexp_substr (txt, ' [de] [^] +', 1, level * 2) s

    t

    connect by level<= regexp_count(txt,="">

  • regexp_substr to search a pattern identical to 'De12022015 Se13022015'

    Hi guys,.

    I have an obligation to get the substring using REGEXP_SUBSTR

    create table test (ID number,

    text varchar2 (200));

    insert into test (id, text) values (1, ' Line1goesHere)

    From12022015 To13022015

    blablablabla');

    insert into test (id, text) values (1, ' Line1goesHere)

    From01012010 To30132016

    blablablabla');

    Select * from test;

    IWant to get the start date and date of the second line of text...

    Query SQL out put must be as shown below

    From12022015 To13022015

    From01012010 To30132016


    Can someone please advice how to achieve this using REGEXP_SUBSTR

      1* select regexp_substr(text, 'From[0-9]{8} To[0-9]{8}') from test
    SQL> /                                                                 
    
    REGEXP_SUBSTR(TEXT,'FROM[0-9]{8}TO[0-9]{8}')
    -----------------------------------------------------------------------
    
    From12022015 To13022015
    From01012010 To30132016
    
  • What command to use?

    I need to make treatment as below.

    Let's say that one

    NAME (VARCHAR2) = JamesBond@NYC.

    I need to separate the NAME into two parts, namely Jean Joly and NYC based on the occurrence of ' @'.


    Please suggest.

    Thank you very much!!

    You can use a / combination of these functions

    INSTR

    SUBSTR

    REGEXP_SUBSTR

    REGEXP_REPLACE

    SQL > t
    2 as
    (3)
    4. Select "JamesBond@NYC" double str
    5)
    6 select ' use INSTR/SUBSTR ' method '.
    7, substr (str, 1, instr(str, '@')-1) str1
    8, substr (str, instr (str, ' @') + 1) str2
    9 t
    10 the union all the
    11. Select 'Use REGEXP_SUBSTR'
    12, regexp_substr (str, ' [^ @] +', 1, 1)
    13, regexp_substr (str, ' [^ @] +', 1, 2).
    14 t
    all 15 union
    16. Select 'Use REGEXP_REPLACE'
    17, regexp_replace (str, ' @. * $')
    18, regexp_replace (str, ' ^. * @')
    19 t;

    STR1 STR METHOD
    -------------------- --------- ---
    Using INSTR/SUBSTR Jean Joly NYC
    With the help of REGEXP_SUBSTR Jean Joly NYC
    Using REGEXP_REPLACE Jean Joly NYC

    SQL >

    Post edited by: Karthick_Arp. Added example

  • regexp_substr - how to search for two different words and result based on returning a different part of the string

    Hello everyone

    I fight with this regexp_substr and I wonder if there is a way to deal with it. I have two string type in a column with the following structure:

    structure 1 always starts with "app.catalog.school.", as the following three examples

    app.catalog.school.DB.BT

    app.catalog.school.B.FTA

    app.catalog.school.ACA.CD

    structure 2 always begins with "app.catalog.admin.", as the following three examples

    app.catalog.admin.C.ABC

    app.catalog.admin.BT.AP

    app.catalog.admin.MI.RT

    If the value of the field begins with "app.catalog.school." it should return everything after the last period (.) as follows

    BT

    FREE TRADE AGREEMENT

    CD

    If the value of the field begins with "app.catalog.admin." it should return the string between the third and fourth period (.) as follows

    C

    BT

    MI

    I actually have a solution to this:

    decode)

    regexp_substr (myfield, ' [^.] +' 1, 3), 'school', regexp_substr (myfield, ' [^.] +' 1, 5), "admin", regexp_substr (myfield, ' [^.] +' 1, 4)

    )

    I am not happy with the solution above using decode(). I think I should be able to solve this problem purely with reg_exp but after hours to try again without success. You have better solution/suggestion?

    I use Oracle version 10.2. Thank you very much.

    Oops: Oracle version 10.2, here to use regexp_replace

    WITH testdata UNTIL
    (SELECT 'app.catalog.school.DB.BT' FROM DUAL str

    UNION ALL

    SELECT 'app.catalog.admin.C.ABC' FROM DUAL
    )

    Select
    Str
    , regexp_replace (str, ' ^. * (admin | school\.)) [ ^.] +)\. ([^.] +). (* $', "\2") res
    of testdata

    RES STR

    "app.catalog.school.DB.BT" 'BT '.

    'app.catalog.admin.C.ABC' 'C '.

    to 11.x, we can use regexp_substr because he has this subexpression setting more pure accident.

    WITH testdata UNTIL
    (SELECT 'app.catalog.school.DB.BT' FROM DUAL str

    UNION ALL

    SELECT 'app.catalog.admin.C.ABC' FROM DUAL
    )

    Select
    Str
    , regexp_substr (str, ' (admin | school\.)) [ ^.] +)\. ([^.] +)', 1, 1, null, res) 2.
    of testdata

    STR RES
    app.catalog.school.DB.BT BT
    app.catalog.admin.C.ABC C

    Post edited by: extended chris227

Maybe you are looking for