For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2008 > How to find this pattern by regular expression?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to find this pattern by regular expression?
Ahmad

2008-03-31, 8:42 am

Hi,

I want to find the pattern: PATTERN that can contain lower case or
upper case characters, digits, and special characters like : _ ; ( )
& .

A sample context is:

-- Start here (this line is not in context)

PATTERN {
@ a line containing anything(comment line)
@ multiple comment lines can exist
// Another type of comments
/* Third type of comments
and it's a multiline comment
*/
x1=a AND b
y1= x1 OUTSIDE layer100
copy y1

//Comments can exist anywhere in the text (between the braces)

// this is the closing brace }

-- End here (this line is not in context)

I need when apply regular expression on the above sample context, it
return the word PATTERN.

Any guidance about the most efficient RE to perform that?

Thanks and best regards,
Ahmad
Gunnar Hjalmarsson

2008-03-31, 8:42 am

Ahmad wrote:
> I want to find the pattern: PATTERN that can contain lower case or
> upper case characters, digits, and special characters like : _ ; ( )
> & .
>
> A sample context is:
>
> PATTERN {
> @ a line containing anything(comment line)
> @ multiple comment lines can exist
> // Another type of comments
> /* Third type of comments
> and it's a multiline comment
> */
> x1=a AND b
> y1= x1 OUTSIDE layer100
> copy y1
>
> //Comments can exist anywhere in the text (between the braces)
>
> // this is the closing brace }


my ($pattern) = /([\w:;()&.]+)\s+{.+?}/s;

> Any guidance about the most efficient RE to perform that?


Efficient? What makes you consider efficiency being an issue?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Tad J McClellan

2008-03-31, 8:42 am

Ahmad <ahmad.abdulghany@gmail.com> wrote:
> Hi,
>
> I want to find the pattern: PATTERN that can contain lower case or
> upper case characters, digits, and special characters like : _ ; ( )
> & .



/[\w:;()&.]+/


> A sample context is:
>
> -- Start here (this line is not in context)
>
> PATTERN {
> @ a line containing anything(comment line)
> @ multiple comment lines can exist
> // Another type of comments
> /* Third type of comments
> and it's a multiline comment
> */
> x1=a AND b
> y1= x1 OUTSIDE layer100
> copy y1
>
> //Comments can exist anywhere in the text (between the braces)
>
> // this is the closing brace }
>
> -- End here (this line is not in context)
>
> I need when apply regular expression on the above sample context, it
> return the word PATTERN.



my($match) = $context =~ /(PATTERN)/;


> Any guidance about the most efficient RE to perform that?



You should first try to find ANY regex that does what you want.

Only if it proves to be too slow should you concern yourself with
making it faster.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com