newbie question: find index of substr using regexp
newbie question: find index of substr using regexp
am 05.10.2007 20:44:58 von seannakasone
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.
Re: newbie question: find index of substr using regexp
am 05.10.2007 20:59:14 von glex_no-spam
Sean Nakasone wrote:
> 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"
Re: newbie question: find index of substr using regexp
am 05.10.2007 21:19:59 von Jim Gibson
In article
,
Sean Nakasone wrote:
> 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.
>
No, index takes only strings, which are not interpreted as regular
expressions.
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 as described in 'perldoc perlre').
You can also put (.*?) at the beginning of your regular expression and
use the length of the captured string $1, but make sure that addition
doesn't screw up your RE.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: newbie question: find index of substr using regexp
am 05.10.2007 21:29:33 von seannakasone
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));
Re: newbie question: find index of substr using regexp
am 05.10.2007 21:45:31 von seannakasone
ok i guess i'll be using this. thanks for your help
use strict;
use warnings;
my $a = " quick brown";
$a =~ /[^ ]/;
print (length($`));
Re: newbie question: find index of substr using regexp
am 05.10.2007 21:53:24 von Jim Gibson
In article
,
Sean Nakasone wrote:
> 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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: newbie question: find index of substr using regexp
am 05.10.2007 23:13:16 von Dummy
Sean Nakasone wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: newbie question: find index of substr using regexp
am 06.10.2007 02:26:27 von Tad McClellan
Sean Nakasone wrote:
> 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 $`
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: newbie question: find index of substr using regexp
am 06.10.2007 02:54:56 von Mintcake
On Oct 6, 2:53 am, Jim Gibson wrote:
> In article
> ,
>
> Sean Nakasone wrote:
> > 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
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
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*//;
Re: newbie question: find index of substr using regexp
am 06.10.2007 12:29:27 von nobull67
On Oct 5, 8:19 pm, Jim Gibson wrote:
> In article
> 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.
Re: newbie question: find index of substr using regexp
am 06.10.2007 13:02:54 von Tad McClellan
Mintcake wrote:
> On Oct 6, 2:53 am, Jim Gibson wrote:
>> In article
>> ,
>>
>> Sean Nakasone wrote:
>> > 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
>>
>> Posted Via Usenet.com Premium Usenet Newsgroup Services
>> ----------------------------------------------------------
>> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
>> ----------------------------------------------------------
>> http://www.usenet.com
[ 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
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: newbie question: find index of substr using regexp
am 06.10.2007 16:56:12 von rvtol+news
Mintcake schreef:
> remove leading whitespace from the string
>
> $a =~ s/\s*//;
No, s/\s+//
--
Affijn, Ruud
"Gewoon is een tijger."
Re: newbie question: find index of substr using regexp
am 06.10.2007 17:21:23 von rvtol+news
Dr.Ruud schreef:
> Mintcake:
>> remove leading whitespace from the string
>>
>> $a =~ s/\s*//;
>
> No, s/\s+//
And s/^\s+// is better altogether.
--
Affijn, Ruud
"Gewoon is een tijger."