regular expression

P

Peter Janssens

Hi,

This is probably a very simple question but I can not solve it.
I want to retrieve all occurences of a string "aa". So in teh search string
"aaaa", I have to find it three times (aa)aa, a(aa), aa(aa).

How can I do that, using regular expressions in Perl.

Thanks.

Hugo
 
I

it_says_BALLS_on_your forehead

Peter said:
Hi,

This is probably a very simple question but I can not solve it.
I want to retrieve all occurences of a string "aa". So in teh search string
"aaaa", I have to find it three times (aa)aa, a(aa), aa(aa).

How can I do that, using regular expressions in Perl.

don't know about regexes, since they 'eat up' the string. there may
still be a way to use them. i would use index and substr:

----
use strict;
use warnings;

my $str = 'aaaabb aa';

my @ind;

for my $itr ( 0 .. length( $str ) - 1) {
push @ind, $itr if ( index( substr( $str, $itr++ ), 'aa' ) == 0 );
}

print "$_\n" for @ind;

__OUTPUT__
1
2
3
8

this gives you the position ( 1-based, not zero-based ) in the string
where the matches occur. there are probably many more elegant solutions
of course...
 
J

John W. Krahn

Peter said:
This is probably a very simple question but I can not solve it.
I want to retrieve all occurences of a string "aa". So in teh search string
"aaaa", I have to find it three times (aa)aa, a(aa), aa(aa).

How can I do that, using regular expressions in Perl.

$ perl -le'print for "aaaa" =~ /(?=(aa))/g'
aa
aa
aa



John
 
P

Paul Lalli

Peter said:
This is probably a very simple question but I can not solve it.
I want to retrieve all occurences of a string "aa". So in teh search string
"aaaa", I have to find it three times (aa)aa, a(aa), aa(aa).

How can I do that, using regular expressions in Perl.

Change your way of looking at it. Instead of "all strings 'aa'", you
want "all strings 'a' which are followed by the string 'a'" :

$_ = 'aaaa';
print "$1$2\n" while /(a(?=(a)))/g;

Take a look at "look-ahead assertions" in `perldoc perlre`.

Note that in the specific case you're referring to, where your pattern
match really doesn't contain any "regular expressions", I agree with
the previous poster - index and substr are probably the way to go. I
give you this answer on the assumption that your real problem is more
complicated than this example.

Paul Lalli
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
ahh! i always forget about the zero-length! nice.

by 'zero-length', i meant 'zero-width assertions' :).
 
P

Peter Janssens

Thanks,

This does the job. I didn't know the look-ahead feature. The regex is in
fact more complex.

Peter
 
D

Dr.Ruud

Peter Janssens schreef:


The Subject of your posting does not mention your question. Please read
the Posting Guidelines.
This is probably a very simple question but I can not solve it.
I want to retrieve all occurences of a string "aa". So in teh search
string "aaaa", I have to find it three times (aa)aa, a(aa), aa(aa).

How can I do that, using regular expressions in Perl.

Wait for Perl 6, or use something like

echo 'aaaa' | perl -lne 'print $1.$2 while /(a)(?=(a))/g'
 
P

Peter Janssens

Hi,

The solution works well for the search-string "aa". But suppose I want to
search for aaa or aab. I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

If you try something like aa[ab] it will find the first instance (aaa) but
not the second (aab) in aaabaabaa.

Your help is appreciated.

Peter
 
T

Tad McClellan

Peter Janssens said:
The solution works well for the search-string "aa". But suppose I want to
search for aaa or aab. I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

If you try something like aa[ab] it will find the first instance (aaa) but
not the second (aab) in aaabaabaa.


----------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'aaabaabaa';
my $search = 'aa[ab]';

while ( /$search/g ) {
print "matched: $`<$&>$'\n";
pos() -= length($&) - 1;
}
----------------------------

Your help is appreciated.


Your composing followups in an acceptable manner would be much appreciated.

Quote context, trim what doesn't matter, and interleave your comments
with the context that the comment applies to, like everybody else
here does.


[ snip TOFU ]
 
X

Xicheng Jia

Peter said:
Hi,

The solution works well for the search-string "aa". But suppose I want to
search for aaa or aab. I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

If you try something like aa[ab] it will find the first instance (aaa) but
not the second (aab) in aaabaabaa.

try this:

echo "aaabaabaa" | perl -nle '/(aa[ab])(?{print $1})(?!)/'

Xicheng
 
M

Mumia W.

Peter said:
Hi,

The solution works well for the search-string "aa". But suppose I want to
search for aaa or aab. I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

If you try something like aa[ab] it will find the first instance (aaa) but
not the second (aab) in aaabaabaa.

Your help is appreciated.

Peter

A paraphrased version of John W. Krahn's idea seems to work best:

local $\ = "\n";
my $value = 'aaabaabaa';
my $rx = 'aa[ab]';

print for $value =~ m/(?=($rx))/g;
 
D

Dr.Ruud

Robert Hicks schreef:
Dr.Ruud:

I think he wants something rather soon?! : )

Or something else:

$ perl -wle'
$_ = "abracadabra" ;
$n = 1 ;
print($1 or "#-$n-")
while /(?=(a(?:.*?a){$n}))/g or ++$n and /a(?:.*?a){$n}/
#-1-'
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top