2 Replies Latest reply: Apr 23, 2007 6:14 AM by 807606 RSS

    Extracting select items using regex

    807606
      As the title suggests, i have an sql statement and i would like to extract the select items from it, so as expected the delimeter would be (,) but i've faced a problem with this statement :
      select 
      FORM_SETTINGS.FORM_ID   ,  IFNULL(DATE_FORMAT(PERSON_PROFILE.HIRING_DATE,'%b %e, %Y'),CHAR(63)),
      IFNULL(DATE_FORMAT(PERSON_PROFILE.HIRING_DATE   ,   '%b %e  ,   %Y'   ),CHAR(63)) , FROMM_SETTINGS.FORM_NAME,FROM_SETTINGS_FROM.DISPLAY_NAME,
      FORM_SETTINGS.ETQ$MODIFIED_DATE AS DATE_MODIFIED
      from
      ENGINE.FORM_SETTINGS AS FORM_SETTINGS
      Inside the select items i have (,) which are basiclly inside the paranethesis so how am i supposed to distinguish ???
      i want the final result (extracted selected items) to be like this:

      FORM_SETTINGS.FORM_ID

      IFNULL(DATE_FORMAT(PERSON_PROFILE.HIRING_DATE,'%b %e, %Y'),CHAR(63))

      IFNULL(DATE_FORMAT(PERSON_PROFILE.HIRING_DATE , '%b %e , %Y' ),CHAR(63))

      FROMM_SETTINGS.FORM_NAME

      FROM_SETTINGS_FROM.DISPLAY_NAME

      FORM_SETTINGS.ETQ$MODIFIED_DATE AS DATE_MODIFIED

      Can anyone help ??? it would very much appreciated ...i've been trying this for days but still can't figure out a way ...I know someone will say you dont have to do it in regex but id love to do it in regex since it would be very much cleander code
        • 1. Re: Extracting select items using regex
          807606
          You could use 'as' to create an alias on each column then use this to identify a column.

          Or you could write a parser that will only delimit when:

          1) the current character is , and
          2) there are no open braces or quotation marks.

          You can check 2 by using a simple counter that increments each time a ( is reacher and decrements each time a ) is reached. when the counter is zero you know you are outside the braces.

          Beware of further pitfalls though such as cases when ( is not a ( but a string such as substring('(')

          I highly recommend you take a look at javacc.
          • 2. Re: Extracting select items using regex
            807606
            well i dont have enough time to look into javacc and their source-code and how it works ...The problem is there's no advanced regex tutorial for java on the net to help me out