Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Help with regex

by reaper9187 (Scribe)
on Sep 19, 2014 at 05:04 UTC ( [id://1101149]=perlquestion: print w/replies, xml ) Need Help??

reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks, I'm back here to seek your guidance and help with regexes. Basically, I'm parsing through an excel file that has whole database of entries as a table. Now, the table has headers corresponding to every column like so :
"Header 1" "Header 2(%)" "header 3 " ......
I need to read through the columns matching these keywords. The catch here is that keywords will be the same, but it may or may not include special characters, upper/lowercase characters,whitespaces in the header.

So, if my keywords are "header1", "header2", and so on,....the regex should be able to identify all possible combinations of the keyword with variations as mentioned above.

Replies are listed 'Best First'.
Re: Help with regex
by hdb (Monsignor) on Sep 19, 2014 at 06:00 UTC

    Remove all non-letter and non-digit characters from your header and then do a case-insensitive comparison. Something like

    $header =~ s/[^a-zA-Z0-9]//g; if( $header =~ /header1|header2/i ) { ...

    UPDATE: changed /I to /i

Re: Help with regex
by GrandFather (Saint) on Sep 19, 2014 at 06:49 UTC

    I thought Excel exported stuff as CSV files so the way to "parse" the data is to use Text::xSV or one of the other CSV munging modules in CPAN designed for exactly that purpose.

    Perl is the programming world's equivalent of English
Re: Help with regex
by biohisham (Priest) on Sep 19, 2014 at 05:46 UTC

    Are those headers enclosed in double quotes ? How many headers are there ? Also it is not clear what delimiters between fields your excel file has but that information will be relevant when thinking about constructing a regex, table files can be comma-separated, tab separated, space-separated ..etc. Are you reading your file one line at a time ? It maybe useful to give a snippet of the code you tried to solve this problem and also a few lines of your file to see how they look like.


    A 4 year old monk
Re: Help with regex
by reaper9187 (Scribe) on Sep 19, 2014 at 06:09 UTC
    Thank you hdb. Thats just what i needed. I'm sorry biohisham is I wasn't clear enough. but i've got the solution now. Thanks and appreaciate your help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101149]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found