Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Localization of a expresion ?

Status
Not open for further replies.

grimbys

Programmer
Dec 19, 2002
34
0
0
ES
Hi! I have a new problem :(

Imagine (this is only a example, i'd like it was general):

$er = "(a) (b\w*) (b\w*) (c)";
$word = "d a b b c d a b b c d";

while ($word =~ /($er)/g)
{
I want to know how many words are there before second b
}

How can I obtain the pos of ($4)?

Thath is to say, I'd like can get :
"d a b" / "b" / "c d a b b c d" at first iteration
there are 3 words before second "b"
and get
"d a b b c d a b" / "b" / "c d" at second iteration
there are 8 words before "second" "b"

Have you got any idea? Thanks
 
I haven't test this extensively but it worked for the following two strings:
Code:
my $string = 'd a b b c d a b b c d';
# my $string = 'dog alf bat ball cat doc fun beach barf chat dams';
my $words = 0;

while ( $string =~ /((?:\w+ )+?b\w*) (?=b)/ig) {

    $words += split /\s+/, $1;
    print "$words words before second 'b'\n";
}
which outputs:
[tt]
3 words before second 'b'
8 words before second 'b'
[/tt]
You didn't specify how to handle cases with three 'b's: 'a d b b b c' so I didn't worry about it.

jaa
 
Thanks for your help!

I looked up about how many words are there before than $i, my post wasn't clear.

I have read perlvar and I saw $- which is a variable thath gives me a solution for my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top