Parse qualified double quote delimited strings

Hi all

I have a few strings of data that I get I want to analyze. My problem is that I am not able to understand how I can analyze the fields that are double quote qualified ("19 999")?

I played with the regex_substr, but I can't quite make what I want.

I want to analyze the myString column in my example below in four areas:
Product
OnOrder
OnHand
TotalSold

Here is an example... I've been playing with it for a bit, and I can't get it.
WITH parseString AS (Select 
                        '"Grado, Headphones",123,2222,"19,999" ' myString
                     from dual
                     UNION ALL
                     Select 
                        'Audio Slave,222,3333,444' myString
                     from dual
                     
                                )
Select 
    myString
from parseString
Oracle Database 10 g Enterprise Edition release 10.2.0.3.0 - 64bi
PL/SQL version 10.2.0.3.0 - Production
CORE Production 10.2.0.3.0
AMT for Linux: release 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production


Any suggestions greatly appreciated.

S

Published by: ScarpacciOne on April 22, 2010 15:24

Hello

As Centinul said, it would help a lot if you have published your desired results and explains how get you from the sample data.
Here's something that can help, according to your specific needs:

WITH     got_item_cnt     AS
(
     SELECT     myString
     ,     1 + LENGTH (myString)
            - LENGTH (REPLACE (myString, ','))     AS item_cnt
     FROM     parsestring
)
,     cntr     AS
(
     SELECT     LEVEL     AS n
     FROM     dual
     CONNECT BY     LEVEL <= ( SELECT  MAX (item_cnt)
                       FROM    got_item_cnt
                     )
)
,     comma_separated_values     AS
(
     SELECT     i.myString
     ,     c.n
     ,     REGEXP_SUBSTR ( i.myString
                     , '[^,]+'
                     , 1
                     , c.n
                     )          AS value
     FROM     got_item_cnt     i
     JOIN     cntr          c     ON     c.n     <= i.item_cnt
)
,     got_quote_cnt     AS
(
     SELECT     myString
     ,     n
     ,     REPLACE (value, '"')     AS value
     ,     NVL ( SUM ( LENGTH (value)
                 - LENGTH (REPLACE (value, '"'))
                 ) OVER ( PARTITION BY  myString
                       ORDER BY      n
                       ROWS BETWEEN  UNBOUNDED PRECEDING
                         AND      1        PRECEDING
                        )
              , 0
              )          AS quote_cnt
     FROM     comma_separated_values
)
SELECT     myString
,     n
,     LTRIM ( SYS_CONNECT_BY_PATH (value, ',')
           , ','
           )          AS token
FROM     got_quote_cnt
WHERE     CONNECT_BY_ISLEAF     = 1
START WITH     MOD (quote_cnt, 2)     = 0
CONNECT BY     MOD (quote_cnt, 2)     = 1
     AND     myString     = PRIOR myString
     AND     n          = PRIOR n + 1
ORDER BY     myString,     n
;

Output of your sample data:

MYSTRING                                N TOKEN
-------------------------------------- -- --------------------
"Grado, Headphones",123,2222,"19,999"   2 Grado, Headphones
"Grado, Headphones",123,2222,"19,999"   3 123
"Grado, Headphones",123,2222,"19,999"   4 2222
"Grado, Headphones",123,2222,"19,999"   6 19,999
Audio Slave,222,3333,444                1 Audio Slave
Audio Slave,222,3333,444                2 222
Audio Slave,222,3333,444                3 3333
Audio Slave,222,3333,444                4 444

The basic strategy is to divide myString to all the commas, then re - unite consecutive elements that are in double - quotes.
You never said what version of Oracle are you uisng.
This solution works in Oracle 10.1. REGEXP_COUNT, introduced in Oracle 11.1, would make it a bit simpler.

Here is each subquery:
got_item_count finds the number of elements by commas in each myString
CNTR generates integers 1, 2, 3,... until the maximum number of elements by commas on line any
comma_separated_values myString splits its different elements, ignoring the quotes at the moment. I assumed myString is unique. If this isn't the case, you need some other unique identifier for each row of parsestring.
* quote_cnt had "notes how quotes took place in myString, each element not included.  If (and only if) this number is odd, then the element is really a continuation of a previous item, in other words, the comma that separates this point from the previous one was within quotation marks and therefore should not have divided myString.  I assumed that the double-quote character always indicates the grouping; It is never to be taken literally, and therefore the final results will never contain quotes.
The main query string aggregation on consecutive points, bringing together all the elements that are weird quote_cnts with the last item previous had a same quote_cnt.

Published by: Frank Kulash, 22 April 2010 23:33

Tags: Database

Similar Questions

  • [JS - CS4] Help w / JS choke in double quotes in strings and regex

    Someone at - it an easy way to extract text from a string with quotes in JavaScript? I seem to have encountered some limitations in the javascript regex implementation and how a script handles a string with quotes in it.

    Here's what I'm working with: a regular as string:
    {appliedParagraphStyle: "Headline 6", changeConditionsMode:1919250519}
    I need to extract just the text "Headline 6"without the quotes

    I came across a regular expression that would get only the text without the quotes, but apparently Javascript does not support lookbacks. For example, wonderful WhatTheGrep of Jongware script can decode this regex:
    ( ? < = ») [^"]*? (?=")
    but ID can't find nothing with it using a GREP search. The same search without the hindsight find stuff:
    "[^"]*? (?=")
    But what he always finds the quote at the beginning of the string.

    I thought I could just use substrings to retrieve the text without the quotation, but apparently he has problems. See these examples from the Javascript console:

    testStr;
    Result: "Headline 6".
    testStr.length;
    Result: 1

    testStr;
    Result: Title 6
    testHL.length;
    Result: 10

    testHL;
    Result: 'title 6 '.
    testHL.length;
    Result: 1

    So if the string contains a quotation mark in it, it does not return the actual length, so I can't encode a beginning and end of the string to extract the part without the quotation.

    One of you more experienced people have a magical idea? TIA!

    Thank you, Jongware, your answer had the keys of a workaround - something I stumbled on in the review of your code.

    As new script, I had indeed crashed on the assumption that regular expressions worked the same in JS and GREP searches. ID can do lookbacks but JS can not - check out www.regular-expressions.info/refflavors.html for a comparison of the regex flavors; JS is listed as ECMA in the table.

    In case you or anyone who wants to know, my problem concerns styles conversion a Word doc that may or may not have styles, the script expects. ID hesitate looking for a style that is not part of the document, so I had to intercept it. Rather than extracting the text from the style of the chain and comparing it to a list of styles for the document, I made a regexp to the list of style and use to match the string, avoiding having to make quotes in there at all.

    var app.activeDocument = docRef;
    var myStyles, s;
    for (var s = docRef.paragraphStyles.length - 1; s > = 2; s-) {}
    get the list styles, but not the default styles in brackets, because they're going to screw up a regular expression pattern
    myStyles = docRef.paragraphStyles [s] .name + "|" + myStyles;
    }

    myRE = new RegExp (myStyles);

    Str = "{findWhat:------"do", appliedParagraphStyle:------"Topic 1\", appliedFont:------"Wingdings\", fontStyle:------"Bold\"}";
    f = str.match (Myrie);
    If (f)

    Alert ("search for:" + f + "work" "");
    on the other
    Alert ("do not attempt a search... ») ;

    Thanks again for your kind response, it was just the additional perspective that I needed!

  • Replace the string, each value should double quotes around it

    How can I replace this basic string with double quotes around each value?

    dog, cat, john, mark

    I would like to:

    'dog', 'cat', 'john', 'brand '.

    Thank you

    -weStsIdE

    Hello

    I answered my own question. Well nice for someone else. The function ListQualify will insert what you need before and after each element of individual list.

    Actually powerful enough that you can just insert double quotation marks, for example around channels and leave intact numeric values.

    -weStSidE

  • REGEXP_REPLACE remove the comma in double quotes

    Hello

    I am trying to remove the comma of a string in double quotes, currently I can only remove a comma using under sql

    SELECT REGEXP_REPLACE (' "" A, B, C, def "', '(") ([^ "|,"] +) (,) ([^ ""] +) ("") ',' \1\2 \4\5' ') FROM DUAL;

    output: 'A B, C, def'

    But I need the form "A B C def".

    Hello

    Jarkko Turpeinen wrote:

    ... I think that I stop to answer here, because I don't understand of course questions

    Very understandable.  If my assumption is correct, the question would be much clearer if OP gave an example that included a few commas inside the quotation marks and some outside, as in the example below.

    Here's a way to implement what I have described in the answer #2 above:

    VARIABLE input_txt VARCHAR2 (100)

    EXEC: input_txt: = 1, '2', ' A, B, C, def ", 3" foo, bar '4 ';

    WITH got_parts AS

    (

    SELECT LEVEL AS part_num

    , REGEXP_SUBSTR (: input_txt)

    , '[^"]*("|$)'

    1

    LEVEL

    ) AS part_txt

    OF the double

    CONNECT BY LEVEL<= 1="" +="" regexp_count="" (="">

    , '"'

    )

    )

    SELECT LISTAGG (CASE

    WHEN MOD (part_num, 2) = 0

    THEN REPLACE (part_txt, ',')

    Of OTHER part_txt

    END

    ) Within AS output_txt GROUP (ORDER BY part_num)

    OF got_parts

    ;

  • Limit the Total number of characters between double Quotes using Oracle 11 g PL/SQL

    Hello

    I need a way to apply a limit of 32 substring for each group of data inside double quotes, with Oracle 11 g PL/SQL.

    Essentially using the following example string that much for me, I have no control of how this string is built:

    str := "AAAAAAA BBBBBBB CCCCCCC DDDDDD1","AAAAAAA BBBBBBB CCCCCCC DDDD",”QWERTY”,”N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1”’;

    I need to go through each group of values between each double quote, for example ("QWERTY") and apply a substring of 32 to each group found.

    So, using the example above of the chain, it's as if I need to do the following:

    str := "substr(‘AAAAAAA BBBBBBB CCCCCCC DDDDDD1’,1,32)","substr(‘AAAAAAA BBBBBBB CCCCCCC DDDD’,1,32)",”substr(‘QWERTY’,1,32)”,”substr(‘N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1’,1,32)”’;

    So in the end, my final result for the str would be composed of four groups where the length of each grouping is less than or equal to 32.

    Any help would be great.

    Thank you.

    Try this (replace with your actual table):

    with the temp as

    (

    SELECT substr (REGEXP_SUBSTR (str,'[^,] +', 1, LEVEL) 1, 32) r

    OF THE TEST

    connect IN REGEXP_SUBSTR(str, '[^,]+', 1, LEVEL) IS NOT NULL

    )

    Select replace (listagg (r, '"," ') in the Group (order by null),' ' "','" ' ")

    temp

    output:

    "VIEN BBBBBBB CASTILLO DDDD", "VIEN BBBBBBB CASTILLO DDDDDD1", "N1N1N1N1N1N1N1N1N1N1N1N1N1N1N1N", "QWERTY".

  • Find and replace the delimited string value by the

    Hi all

    I have a requirement where I need to find and replace the delimited string values.

    For example, the string is "GL ~ 1001 ~ 157747 ~FEB-13~ CREDIT ~ A ~ N ~ US ~ NULL ~". The 4th column gives the month and year. I need to replace it with the name of the previous month. For example: "GL ~ 1001 ~ 157747 ~JAN-13~ CREDIT ~ A ~ N ~ US ~ NULL ~". I need to do the same thing for the past 12 months.

    I thought initially divide the values and store it in a variable and then after him substituting the value required, join the return.

    I just wanted to know if there is a better way to do it?

    Like this:

    with a model like

    (select "GL ~ 1001 ~ 157747 ~ FEB-13 ~ CREDIT ~ A ~ N ~ $ ~ NULL ~' double UNION ALL data")

    Select ' GL ~ 1001 ~ 157747 ~ JAN-13 ~ CREDIT ~ A ~ N ~ US ~ NULL ~' double data)

    Select

    REPLACE (DATA, TO_CHAR (to_date (substr (data, 16.6), "MON-RRRR"), 'MON - RR'), TO_CHAR (to_date (substr (data, 16.6), "MON-RRRR")-1, 'MON - RR'))

    modeling;

    GL ~ 1001 ~ 157747 ~ JAN-13 ~ CREDITS ~ HAS ~ N ~ US ~ NOTHING ~

    GL ~ 1001 ~ 157747 ~ DEC-12 ~ CREDITS ~ HAS ~ N ~ US ~ NOTHING ~

    Ishan

  • Problem with the removal of the double quotes in my channel

    Hi all

    I am trying to replace two double quotes in my thong in double apostrophe.

    < v21:ID schemeAgencyID = "" NGCRM"" >


    I need to convert it to < v21:ID schemeAgencyID = "NGCRM" >


    For that, I use my code as below.

    Input=input.replace("\"\"","\" ");

    but there is no change in the output.

    Kindly tell me the error in the present.



    Thank you
    Sabarisri. N

    Published by: N Sabarisri August 1, 2011 12:20

    Sabarisri N wrote:
    Hi all

    I am trying to replace two double quotes in my thong in double apostrophe.

    I need to convert it to

    For that, I use my code as below.

    Input=input.replace("\"\"","\" ");

    but there is no change in the output.

    Kindly tell me the error in the present.

    Thank you
    Sabarisri. N

    Published by: N Sabarisri August 1, 2011 12:20

    It'll work. Here is the test program

    class StringReplaceTest
    {
         public static void main(String st[])
         {
              String st1 = "";
              System.out.println(" The st1 : "+st1);
              System.out.println(" The out put is : "+st1.replace("\"\"","\""));
    
         }
    }
    

    replace the text makes the sequence checking and replaceAll of character against the regex mill.

  • Quote modification value chain "to double quote &amp; quot;

    I create a list with the different codes of law within the single quotes as follows
    < cfset corlist = "'1100', ' 1200' ', ' 1300 '", ' 1700 ' ", ' 1800 '", ' 1950 ', '7001' ">" "
    When I do an output
    for
    < cfoutput > AND idbillcode in (#corlist #) < / cfoutput >

    Get the values as follows
    AND idbillcode IN ('1100', ' 1200' ", ' 1300 '", ' 1700 ' ", ' 1800 '", ' 1950 ' ", ' 7001 ')


    However when I put the same string in a cfquery apostrophes get replaced by double quotes as follows

    AND idbillcode IN ("1100", "1200", "1300", "1700", "1800", "1950", "7001") that generates an error.

    Anyone have clues.

    Thank you.

    I found the answer PreserveSingleQuotes. Thank you

  • replace a single quote with the double quotes with the function replace

    I work with one apex page, which contains a text as a filter box. If I put a single quote and find then I get the error message saying that parse error.

    For this, I want to replace one single quotation mark with double quotes, so this should be perfect. and apex should show the result.

    A allow the query to the same which will replace the single quote with double quotes.

    Published by: Santhosh Kumar T 15 Sep 2008 05:13

    If it was confusing... Try the less confusion (according to you)! ...

    Select Replace ("ab" | ") "|' it," ',' "') twice;"

  • OPA-E00111 (unable to use double quotes)

    I am trying to use a table to decide on the error messages for the different scenarios. as

    Header 1
    the error message

    "it cannot display the page" (conclusion).                                    the url is not valid (condition)

    Here, if I use double quotes as 'page 'cannot' see' throw up error

    whenever he tries to use double quotes inside the outcome, it throws an error OPA-E00111.

    can someone help me.

    Thanks in advance

    You must precede the quotation by the backslash character:

    See http://docs.oracle.com/html/E64009_01/toc.htm#Variables%20and%20constant%20values/Use_constant_values_in_rules.htm?Highlight=backslash

  • Fields Publisher report with output .csv just double quotes. How we prevent it?

    Hi all

    If we create any report Publisher and you try to view or download .csv format, fields that have data with more than one word just double quotes. If data have one word, she comes out with proposals and seems correct.

    If we look in excel or data XML seems good with on proposals and did not observe this behavior.

    Version: 11.1.1.5

    Steps to reproduce:


    1 create a model of simple data in select * from scott.dept

    2. create a blank layout and a report to display the data.

    Note: Varchar Fields so has more than one word, it is another to one word in quotes it look ok.

    Sample:

    DEPTNO, DNAME, LOC

    10, ACCOUNTING,"NEW YORK"

    20, RESEARCH, DALLAS

    30, SALES, CHICAGO

    40, OPERATIONS, BOSTON

    Let know if anything missed on my side. Pointers or suggestions would be appreciated.

    Hi all, just re-collected has nothing to do with OBIEE. By default, .csv apply quotes on all fields with special characters or spaces according to its characteristics. Since this is a valid scenario from the point of view OBIEE too, I have to use scripts to remove the quotation marks. Because my data do not have quotes, I remove all citations of my reports after being delivered. Using command on my unix below box and wrote a script based on my needs and expected. $sed-i of / ' / / g' Report1.csv thank you Kiss.

  • Regular expression, replace all commas between double quotes ONLY

    Hello

    I want to remove all commas between double quotes:

    I tried under model, but it does not replace all if we have more than one comma in double quotes.

    SELECT REGEXP_REPLACE (123, "45.6" Hello, "John, Mike, Marc", 456, "-Anne, Anna"', ' "([^"]*),([^"]*)" ', '"\1;\2" ', 1, 0) suite

    FROM DUAL;

    Result

    --------------------------------------------------------------------------

    123, '45; 6"," Hello, "John, Mike;" "Marc", 456, "Anne-; Anna.

    the first comma between 'John, Mike; Marc"is not replaced...

    Recursion can do if you insist on the SQL solution

    with

    correct (INP, orig, CNT, res, Step) as

    (select str,

    substr(STR,2),

    -case when substr (str, 1, 1) = ""' then 1 else 0 end,

    substr (STR, 1, 1),

    1

    of (select 123, '45.6,' Hello, ' John, Mike, Marc ", 456, '-Anne, Anna', 'single_value' ' str of union double all the)

    Select "123456789" across double Union

    Select ' ""' of all the double union

    Select "" 123,456,789"" Union double all the

    Choose 123, '45.6,' Hello, ' John, Mike, Marc ", 456, 'Ann-Marie, Anna', 'unique value' ' double str

    )

    Union of all the

    Select the inp

    substr(orig,2),

    CNT + case when substr (orig, 1, 1) = ""' then 1 else 0 end,

    RES | cases where substr (orig, 1, 1) = ','

    so to case when mod(cnt,2) = 1

    then ' ~'

    else «»

    end

    of another substr (orig, 1, 1)

    end,

    Step + 1

    Rectifier

    where orig is not null

    )

    Select the inp, res

    Rectifier

    where the orig is null

    INP RES
    "" ""
    123456789 123456789
    "123456789". "123 ~ 456 ~ 789".
    123, '45.6' Hello, ' John, Mike, Marc ", 456, '-Anne, Anna', 'single_value '. "123," "45 ~ 6 ', Hello," John ~ Mike ~ Marc ", 456," Anne-~ Anna ',' single_value '.
    123, '45.6' Hello, ' John, Mike, Marc ", 456,"Ann-Marie, Anna","unique value ". "123," "45 ~ 6 ', Hello," John ~ Mike ~ ~ ~ Marc ', 456,' Anne-Marie ~ Anna "," unique value ".

    Concerning

    Etbin

  • Double quotes in FILE SET LINE UTL

    My program writes the data in the CSV file in function below.

    Varial buffer is assigned

    buffer: = A | ','|| B;

    And, the output file contains, lets say, as below

    Apple, boy

    Now let's say that this requirement is to write the value b in double quote,

    Apple, 'Boy '.

    How do I get there?

    Thank you in advance,

    Srini

    Try the below

    buffer: = A | ','|| CHR (34) | B | CHR (34);

  • ODI to Essase Metadata - formula with double quote error.

    Hi all

    We update Essbase Metadata ODI. We have sql table where all requirements of columns for metadata download is here.

    The formula is to have "quotes" in the formula column.

    When he get transferred to Essbase. The double quotes are not getting updated and so the formula is seen as loaded error formula.

    So how do you remedy this? Please suggest.

    Thank you
    Alex keny.

    "' Try to use something like -" formula or member name \ ".

    See you soon

    John
    http://John-Goodwin.blogspot.com/

  • remove the double quotes in a value

    DB version: 10g / 11g

    Table customer_address
    column: adresse_rue
    data in the example: 'MLK 1234 drive '.
    'dr North 150, st #600'
    'o Henry 15678 dr.

    Update: reader of MLK 1234
    150 north dr, st #600
    Dr. Henry o 15678

    I'm looking to remove the double quotes (""), if this is the first and the last character.

    It would be nice to get these lines with values begins and ends with double-quotes


    Thank you for your time

    Published by: nydba on March 22, 2012 09:02

    One way:

    with a as (select '"1234 MLK drive"' x from dual
               union select '5678 JFK drive' from dual)
    select a.*,substr(x,2,length(x)-2) from a
    where x like '"%"';
    
    X                SUBSTR(X,2,LENGTH(X)-2)
    ---------------- -----------------------
    "1234 MLK drive" 1234 MLK drive        
    

    There is probably a more efficient way with regular expressions.

Maybe you are looking for