::: Textpad Regex Question - "RE"al Mind Teaser:::

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
vamshireddy007
Posts: 5
Joined: Fri Sep 19, 2003 2:22 am

::: Textpad Regex Question - "RE"al Mind Teaser:::

Post by vamshireddy007 »

:evil: Hi Folks, :evil:
Is there a regex that
* Can match multiline string where there exist nested tokens. I guess this question can be better illustrated with an example.
Here is an example multiline string that should be matched:
----------- BEGIN -------------
(define MyFunction
(parameters_list (sdsd asdad asdad ) )
(ONE MORE STATEMENT)
)
----------- END -------------
The regex should match all the lines here (perhaps excluding BEGIN and END lines).

The challenge here is to make regex **varying line count** and **nested tokens**.

Cheers,
Gurram
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

I'm sorry. I have no idea of what you want......wanna try a more specific explanation?
vamshireddy007
Posts: 5
Joined: Fri Sep 19, 2003 2:22 am

::: Textpad Regex Question - "RE"al Mind Teaser:::

Post by vamshireddy007 »

Hi Bob,
Looking for a regex that matches a specified sring by taking into consideration of nested characters. In my case, the nested characters are "(". For example if I have the following in a file:

-------------- BEGIN -------------
(line 001)
(line 002)
(line 003-nested (element 003.1)
(element 003.2 (element 003.2.1))
)
-------------- END -------------
The supposed regex to find what I want might look as below:
"/(line 00<SOME_CLEVER_REGEX>" (without quotes)

Resultant match 1, 2, 3 will be:

(line 001)

and

(line 002)

and

(line 003-nested (element 003.1)
(element 003.2 (element 003.2.1))
)

respectively. The third match is in three lines (3 to 5).
This is because the regex matches corresponding "(" and ")" characters.
Similar to the "Search->Match Bracket" menu function in textpad.

Cheers,
Gurram
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Too much to cover here, but check out pages 328-331 of "Mastering Regular Expressions, 2nd Edition" by J. Friedl. It provides a techique to do that using a Dynamic RegEx in perl.

Brief synopsis of theory of solution:
1. Define variables that represent a RegEx for a matched pair. One variable for each nest expected.
2. Include the variable for each nest in each following nest variable.

Example:
my $Level0 = qr/ \( ( [^()] )* \) /x; # paranthesized text
my $Level1 = qr/ \( ( [^()] $Level0 )* \) /x; #one level of nesting
my $Level2 = qr/ \( ( [^()] $Level1 )* \) /x; #two levels of nesting
my $Level3 = qr/ \( ( [^()] $Level2 )* \) /x; #three levels of nesting
.....
.....
my $LevelN = .......... #N levels of nesting

This concept means it could be unlimited by including itself resulting in:
my $LevelN; #must predeclare since it is called in its own expression.
my $LevelN = qr/ \( ( [^()] | (??{ $LevelN })* \) /x;

And by using If statements, you can make this work if there MAY be nested groups vs. if the ARE nested groups.

You could jump right to the end of the explanation on page 331, but I think it is important to understand what is being done, so I would encourage you to digest the whole section, line by line.

I am pretty sure that you cannot do this in TextPad by itself for Searching, but maybe this will provide you another sensible solution.

Hope this was helpful...............good luck,
Bob
vamshireddy007
Posts: 5
Joined: Fri Sep 19, 2003 2:22 am

::: Textpad Regex Question - "RE"al Mind Teaser:::

Post by vamshireddy007 »

Hi Bob,
Thx!!
I'll try and get that book and explore possible solutions.

ALSO, I'LL TRY AND EXPLORE IF [TEXTPAD MACROS] && [CTRL+M] CAN BE USED TO FIND A SOLUTION.

Thx again.

Cheers,
Gurram
Post Reply