newbie question: find index of substr using regexp

S

Sean Nakasone

Hello, i think this should be an easy question. how do i search for a
substring in a string by using a regular expression and obtain the index
of the substring.

i would think i could just use index(), and use a regular expression as
the 2nd parameter, but that doesn't seem to work.
 
J

J. Gleixner

Sean said:
Hello, i think this should be an easy question. how do i search for a
substring in a string by using a regular expression and obtain the index
of the substring.

i would think i could just use index(), and use a regular expression as
the 2nd parameter, but that doesn't seem to work.

perldoc perlre
perldoc -f pos

Also, to see what index supports: perldoc -f index
"...searches for one string within another"
 
S

Sean Nakasone

Ok, i read it. Is this the easiest way to find the offset of the first
non-space character?


use strict;
use warnings;

my $a = " quick brown";
$a =~ m/[^ ]/g;
print (pos($a));
 
S

Sean Nakasone

ok i guess i'll be using this. thanks for your help


use strict;
use warnings;

my $a = " quick brown";
$a =~ /[^ ]/;
print (length($`));
 
J

John W. Krahn

Sean said:
Hello, i think this should be an easy question. how do i search for a
substring in a string by using a regular expression and obtain the index
of the substring.

i would think i could just use index(), and use a regular expression as
the 2nd parameter, but that doesn't seem to work.

perldoc perlvar


Look for the variables @+ (@LAST_MATCH_END) and @- (@LAST_MATCH_START).



John
 
T

Tad McClellan

Sean Nakasone said:
ok i guess i'll be using this. thanks for your help


use strict;
use warnings;

my $a = " quick brown";
$a =~ /[^ ]/;
print (length($`));


Try that with

$a = ' ';

and then with

$a = 'x';

You must ensure that the match _succeeded_ before relying on
the value of $`
 
M

Mintcake

Sean Nakasone said:
ok i guess i'll be using this. thanks for your help
use strict;
use warnings;
my $a = " quick brown";
$a =~ /[^ ]/;
print (length($`));

Don't use any of the RE special variables unless the match succeeded:

if( $a =~ /[^ ]/ ) {
print (length($`));

}

--
Jim Gibson


----------------------------------------------------------

----------------------------------------------------------
color]

This is just a shot in the dark but it sounds to me like you just want
to remove leading whitespace from the string - in which case...

$a =~ s/\s*//;
 
B

Brian McCauley

You can use the length of the $` variable, which is set to the
substring to the left of the match (but be aware of the speed penalty
for doing so

.... so use $-[0] instead. $-[0] has the same value as length($`)
without the penalty.
 
T

Tad McClellan

Mintcake said:
Sean Nakasone said:
ok i guess i'll be using this. thanks for your help
use strict;
use warnings;
my $a = " quick brown";
$a =~ /[^ ]/;
print (length($`));

Don't use any of the RE special variables unless the match succeeded:

if( $a =~ /[^ ]/ ) {
print (length($`));

}


[ It is bad 'net manners to quote .sigs ]

This is just a shot in the dark


It sure is.

It does not answer the question that was asked.

It does not correctly answer the wrong question either...

but it sounds to me like you just want
to remove leading whitespace from the string -


No, the OP wants to "find index of substr using regexp" just
like the Subject header says.

Nowhere in this thread is there anything about removing any characters.

in which case...

$a =~ s/\s*//;


What is the point of replacing zero characters with zero characters?

That removes the first run of whitespace characters, whether they
are "leading" or not.

$a =~ s/^\s+//; # remove leading whitespace
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top