473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regular Expression pattern help needed!

Regular Expression help needed
--------------------------------------------------------------------------------

I want to extract all text that is contained inside the brackets after the word INDIRECT. There must be at least one pair of parentheses, they may be multiple pairs

for exampple, this string

'=IF($B$9="Off",0,INDIRECT(ADDRESS(1(8),COLUMNS($A $41:C41),,,$B$9 )),()aaaa'

has four bracket pairings until the initial "(" is closed out

so the Regexp woul return

INDIRECT(ADDRESS(1(8),COLUMNS($A$41:C41),,,$B$9))

Thanks
Oct 9 '06 #1
1 1711
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. our $parenthesis = qr{
  4.     \(
  5.     (?:
  6.         (?> [^()]+ )
  7.     |
  8.         (??{ $parenthesis })
  9.     )*
  10.     \)
  11. }sx;
  12.  
  13. our $indirectContents = qr{
  14.     INDIRECT\(
  15.     (
  16.         (?:
  17.             (?> [^\(\)]+ )
  18.         |
  19.             (??{ $parenthesis })
  20.         )*
  21.     )
  22.     \)
  23. }sx;
  24.  
  25. my $str = '=IF($B$9="Off",0,INDIRECT(ADDRESS(1(8),COLUMNS($A$41:C41),,,$B$9 )),()aaaa';
  26.  
  27. print "String = $str\n";
  28.  
  29. if ($str =~ m{$indirectContents}) {
  30.     print "Contents = $1\n";
  31. }
  32.  
  33. 1;
  34.  
  35. __END__
  36.  
What you've requested is actually the most advanced thing that perl regular expression allow you to do. I leave it up to you to look up all the resources to explain why the above code works, as I don't even remember where I originally learned how to do this. This code currently prints out what is inside INDIRECT(.*). If you want to include the text "INDIRECT", simply move lines 15 and 21 to 14 and 22 respectively.
Oct 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Tina Li | last post by:
Hello, I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum recursion limit exceeded". Here is the pattern string: ...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Johann Sijpkes | last post by:
Hi, Just before going mad I thought asking this newsgroup would be a good idea. I want to validate dates using a schema but somehow the regexp is not performed by the validator? (at w3c..). I...
26
by: Shannon Jacobs | last post by:
Sorry to ask what is surely a trivial question. Also sorry that I don't have my current code version on hand, but... Anyway, must be some problem with trying to do the negative. It seems like I get...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
6
by: rorymo | last post by:
I have a regular expression that allows only certain characters to be valid in an xml doc as follows: <xs:pattern value="^*" /> What I want to do is also allow any unicode character that is...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.