Regexp help

M

mike

Hi,

I need to find if the line before 'public void aMethod()' has a

// or */

I have tried the following:

if($current_line =~ /(?:\\\|\*\\)/){

}

br,

//mike

//here is some text
public void aMethod(){

}

or

/**
* Here is some text
*/
public void aMethod(){

}
 
J

Jürgen Exner

mike said:
I need to find if the line before 'public void aMethod()' has a

// or */

for my $i (1..$#lines) {
if (index($lines[$i], 'public void aMethod()') >=0) {
print 'Line ', $i-1, " contains //\n"
if (index($lines[$i-1], '//') >=0);
print 'Line ', $i-1, " contains */\n"
if (index($lines[$i-1], '*/') >=0);
}
}

HTH

jue
 
C

C.DeRykus

Hi,

I need to find if the line before 'public void aMethod()' has a

// or */

I have tried the following:

if($current_line =~ /(?:\\\|\*\\)/){

}

br,

//mike

//here is some text
public void aMethod(){

}

or

/**
* Here is some text
*/
public void aMethod(){

}

With 5.10, you could use lookbehind'x \K :

my $qr = qr[ ( # start look-behind
(?: # non-capturing
// # alternative 1
| # or
\*/ # alternative 2
) # end non-capturing
.* \n\K # rest of line
) # end look-behind
public \s+ void \s+ aMethod\(\){
]x;

if ( $current_line =~ /$qr/ ) {
...
 
M

mike

In our last episode,
the lovely and talented mike
broadcast on comp.lang.perl.misc:


Why do you think a backslash, even when escaped with a backslash, will match
a forward slash?

You are absolutely correct I missed it.

Thanks!

//mike
 
M

mike

I need to find if the line before 'public void aMethod()' has a
I have tried the following:
if($current_line =~ /(?:\\\|\*\\)/){



//here is some text
public void aMethod(){


/**
* Here is some text
*/
public void aMethod(){

With 5.10, you could use lookbehind'x \K :

my $qr = qr[ (           # start look-behind
                (?:      # non-capturing
                  //     #   alternative 1
                  |      #       or
                  \*/    #   alternative 2
                )        # end non-capturing
                .* \n\K  # rest of line
             )           # end look-behind
             public \s+ void \s+ aMethod\(\){
            ]x;

if ( $current_line =~ /$qr/ ) {
  ...

Really nice with comments so this was easy to understand and learn
something from.

//mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top