Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

regex newbie

Last post 07-14-2008, 7:18 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  07-10-2008, 8:03 PM 44010

    regex newbie

    how do i parse the data from the following string:

        "result_1",
        'FIRST',       "J & J",
        'LAST',        "Eliab",

     

    I need to save the first name, and the last name and the "1" from "result_1"

    If possible, i don't wnat the spaces between "first" and "J & J"

     thanks.

  •  07-10-2008, 10:52 PM 44015 in reply to 44010

    Re: regex newbie

    I realise it is in a different part of the forum, but can you please read the Posting Guidelines at http://regexadvice.com/forums/thread/43218.aspx and provide us with the necessary information.

    While you are at it, there are some obvious questions:

    - is there always an underscore before the digit in the first field?
    - is the text 'result_' always the same or can it differ?
    - is there always only 1 digit after the '_' or can there be many (max number?)
    - are there always 5 fields? If not, how can we tell when a set of fields starts and ends?
    - are they actually on 3 lines or can they be on any number of lines?
    - is there really a comma after the last field?
    - are the FIRST and LAST words always the same?
    - is there any significance in the use of single and double-quotes
    - will there be a single set of 5 fields or will there be multiple sets to be processed at once?

    There are more things we need to know so that we can answer properly, but that should get us started!

    Susan
     

  •  07-10-2008, 11:47 PM 44016 in reply to 44015

    Re: regex newbie

    Sorry... here's the info to your questions in the order you've listed them.  

    1.  yes.

    2.  same

    3.  the max is 3 digits.

    4.  there are 25 fields, if you count the way you're counting.  I see them as 12 fields with corresponding values... and an ID label.

    5  they are on separate lines.

    6.  no comma after the last field, but I only need the first 13 fields.

    7.  yes.

    8.   the field name is always in single quotes... and the value in double.

    9.  multiple sets.  but I've already got the pattern to locate each set:

    using the following:

    (?:oas.swap_values)(?<Text>[^<]+)(?:)

     

    Finally, a better sample of the text I'm dealing with would look like:

    <script type="text/javascript">
          oas.swap_values(
        "result_1",
        'FIRST',       "J",
        'LAST',        "B",
        'ADDRESS',     "17 abc street",
        'CITY',        "Brampton",
        'STATE',       "ON",
        'FIRST_11C',   "testvalue",
        'LAST_11C',    "s",
        'ADDRESS_11C', "aS",
        'CITY_11C',    "22",
        'STATE_11C',   "ON",
        'STREET_11C',  "j",
        'HOUSE_11c',   "c"
       );
      </script>

  •  07-14-2008, 7:18 PM 44136 in reply to 44016

    Re: regex newbie

    You still have not said what regex or language you are using so I'm assuming that it is a fairly basic one. The pattern:

    (?:oas.swap_values)
    \(   \s*
    "result_(\d{1,3})",
    (
      \s+
      (
        'FIRST',\s+"([^"]*)"
        |
        'LAST',\s+"([^"]*)"
        |
        '[^']*',\s+"[^"]*"
      )
      ,?
    )*
    \s* \)

    (I've used the 'ignore case' and 'ignore whitespace' options). The pattern is split over several lines so that you can see the various parts of it, but you will probably ened to put it onto a single line and remove all of the spaces.

    The text in match group #1 will be the number after 'result_', match group #4 will contain whatever comes after 'FIRST' (with the double-quotes removed) and match group #5 will contain whatever comes after 'LAST' (same deal). I've  not assumed that the order of the fields is the same which is why the whole match actually extends to the closing parenthesis. Also, if there are other field values that you want to extract, then you can extend the overall structure of the pattern.

    If there are multiple entires in the file, then you can expect a match for each entry.

    Susan 

View as RSS news feed in XML