#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Thumbs down Regular expression based on position


    Hi,

    I have a sample input file as follows, with columns Id,Name,Date,Description,Location(separated by ';')

    220;John;10/12/2012;Working as a Professor in University;Hyderabad
    221;Paul;15/08/2012;He is a Software engineer at MNC;Bangalore
    222;Emma;25/01/2013;Working as a mechanical enginner;Chennai

    Like this It contains 30 lines of data.
    My requirement is to extract fields based on position and delimiter using regular expression in eclipse ide from the above text file.

    For example : I want to extract all dates based on the position(3) in the text file, like that i want to extract all the descriptions(4)
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    What you have there is basically a CSV file, so you shouldn't require a regexp at all. You should be able to simply split each string using the semicolon as a delimeter (assuming a semicolon can't appear within any field).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Thumbs down Regular expression based on position


    Hi,

    My requirement is to extract fields from a csv file. We have tried many regular expressions but they didn't exactly match with my requirement. I think we can write regular expression for csv file.
    For Example I have tried with this regular expression (?>;).*(?>;)
    it resulting all the three fields in the middle, excludes only first and last fields.
    Can you suggest me how can i modify my regular expression to get only one field.
    Last edited by ishnid; February 21st, 2013 at 07:33 AM. Reason: Turn off emoticons so regexp will display.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Which language are you using?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Originally Posted by vittal
    I have a sample input file as follows, with columns Id,Name,Date,Description,Location(separated by ';'))
    Code:
    --Capture the 2nd column
    ^[[:space:][:alnum:]_]*;([[:space:][:alnum:]_]*);
    --Capture the 3rd column
    ^[[:space:][:alnum:]_/]*;[[:space:][:alnum:]_/]*;([[:space:][:alnum:]_/]*)

    and so forth.

    It would be a lot easier to extract the info in a perl script.
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Originally Posted by spacebar208
    It would be a lot easier to extract the info in a perl script.
    Or with any language that includes a split/explode function. Or by pasting the file into a spreadsheet application (if it's only a one-off). I'm not convinced that a regexp is the right tool for this job.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Yes, I agree, a split function (or equivalent) would be the easiest. This is why I asked which language.

IMN logo majestic logo threadwatch logo seochat tools logo