LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-29-2012, 01:40 PM   #1
Boomn4x4
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
Perl REGX match between parentheses


I have a script that reads a file that looks as such.
First(James) Zip(1234)
Last(Jones)

I have function that you pass a item to find (such as "First"). A simple regex parses the file and returns the data. /$data\((w+)\)/

This works find, but now there are some items that aren't words, some have spaces and commas in them. How can I return the data between the parentheses regardless of what text is between them?
 
Old 11-29-2012, 02:33 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Code:
/$data\(([^)]+)\)/
Markus

Last edited by markush; 11-30-2012 at 12:41 AM. Reason: typo
 
Old 11-29-2012, 06:40 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by Boomn4x4 View Post
I have a script that reads a file that looks as such.
First(James) Zip(1234)
Last(Jones)

I have function that you pass a item to find (such as "First"). A simple regex parses the file and returns the data. /$data\((w+)\)/

This works find, but now there are some items that aren't words, some have spaces and commas in them. How can I return the data between the parentheses regardless of what text is between them?
The regular expression can be:

Code:
/\((.*?)\)/
#   $1
- '$1' will contain the data you need to be extracted between parenthesis.
 
Old 11-29-2012, 07:43 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,390
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Quote:
Originally Posted by markush View Post
Code:
/$data\(([^(]+)\)/
Markus
Shouldn't that be:
Code:
/$data\(([^)]+)\)/
??

I use the same pattern myself for parsing text with opposing tokens like parentheses, curly braces, and brackets.
--- rod.
 
Old 11-29-2012, 08:32 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by theNbomr View Post
...
I use the same pattern myself for parsing text with opposing tokens like parentheses, curly braces, and brackets.
--- rod.
If we are generalizing, then why not http://perldoc.perl.org/Text/Balanced.html ?
 
Old 11-30-2012, 12:42 AM   #6
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Quote:
Originally Posted by theNbomr View Post
Shouldn't that be:
Code:
/$data\(([^)]+)\)/
??...
Thanks theNbomr, I've changed it.

Markus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl DBI version does not match zuzoa Linux - Software 3 06-24-2012 08:37 PM
Use of uninitialized value in pattern match - perl casperdaghost Programming 4 06-27-2011 07:31 PM
[SOLVED] Regx: Multi line pattern matching angel115 Programming 15 01-04-2011 11:43 AM
String match in perl user_28 Programming 7 09-16-2010 12:37 PM
using sed with parentheses in perl beebop Linux - General 2 07-21-2005 10:41 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration