LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl REGX match between parentheses (https://www.linuxquestions.org/questions/programming-9/perl-regx-match-between-parentheses-4175439346/)

Boomn4x4 11-29-2012 01:40 PM

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?

markush 11-29-2012 02:33 PM

Code:

/$data\(([^)]+)\)/
Markus

Sergei Steshenko 11-29-2012 06:40 PM

Quote:

Originally Posted by Boomn4x4 (Post 4839745)
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.

theNbomr 11-29-2012 07:43 PM

Quote:

Originally Posted by markush (Post 4839768)
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.

Sergei Steshenko 11-29-2012 08:32 PM

Quote:

Originally Posted by theNbomr (Post 4839885)
...
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 ?

markush 11-30-2012 12:42 AM

Quote:

Originally Posted by theNbomr (Post 4839885)
Shouldn't that be:
Code:

/$data\(([^)]+)\)/
??...

Thanks theNbomr, I've changed it.

Markus


All times are GMT -5. The time now is 02:13 AM.