The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Regular expression based on position
Discuss Regular expression based on position in the Regex Programming forum on Dev Shed. Regular expression based on position Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 21st, 2013, 05:16 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 20 m 24 sec
Reputation Power: 0
|
|
Other - 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)
|

February 21st, 2013, 05:53 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
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).
|

February 21st, 2013, 06:23 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 20 m 24 sec
Reputation Power: 0
|
|
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.
|

February 21st, 2013, 12:36 PM
|
|
|
|
Which language are you using?
|

February 21st, 2013, 05:29 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
Quote: | 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.
|

February 22nd, 2013, 05:19 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
Quote: | 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.
|

February 22nd, 2013, 10:50 AM
|
|
|
|
Yes, I agree, a split function (or equivalent) would be the easiest. This is why I asked which language.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|