regular expression again

P

Peter Janssens

Hi,

Original question.
I want to retrieve all occurences of a string "aa". So in the search string
"aaaa", I have to find it three times (aa)aa, a(aa)a, aa(aa).

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

The following solution (previous post) works fine but ...
/(a(?=(a)))/g

Suppose I want to search for aaa or aab in the string aaabaabaa.

I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

Also, 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
 
J

jl_post

Peter said:
Original question.
I want to retrieve all occurences of a string "aa". So in the search string
"aaaa", I have to find it three times (aa)aa, a(aa)a, aa(aa).

The following solution (previous post) works fine but ...
/(a(?=(a)))/g

If you say it works fine, I'll take your word for it... but when I
try this:

print "$1 " while "aaaa" =~ m/(a(?=(a)))/g

I get the following output:

a a a

I see it's capturing three strings (like you wanted), but it's only the
first letter of those strings (which may or may not be what you
wanted). You seem to be content with that, so I'll just assume for the
time being that that's okay with you.
Suppose I want to search for aaa or aab in the string aaabaabaa.

I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

This isn't working because the "(?=a)" is a positive look-ahead
(look it up in "perldoc perlre" if you don't know what that is) that is
telling the regular expression that the next character MUST be "a".
But then the "[ab]" tells it that the next character can either be an
"a" or "b".

So the only way to satisfy both conditions is if the character after
the first "a" is also an "a". I think you meant to write this instead:

a(?=a[ab])

This will find an "a" followed by either "aa" or "ab".

You can see this if you run the following code:

print "$-[0] " while "aaabaabaa" =~ m/(a(?=a[ab]))/g

The output is:

0 1 4

meaning that matches in string "aaabaabaa" were found at indices 0, 1,
and 4. (The "$-[0]" is part of the "@-" array. If you don't know what
it does, you can look it up in "perldoc perlvar".)

I hope this helps, Peter.

-- Jean-Luc
 
T

Tad McClellan

Peter Janssens said:
Original question.


Did you read the entire other thread?

I want to retrieve all occurences of a string "aa". So in the search string
"aaaa", I have to find it three times (aa)aa, a(aa)a, aa(aa).

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


The way that I showed in the original thread is one way.

If that way doesn't work for you, then tell use why not, and we
might be able to work around whatever the issue is...

Suppose I want to search for aaa or aab in the string aaabaabaa.


The code I posted does that.

Your help is appreciated.


If you say so.
 
X

Xicheng Jia

Peter said:
Hi,

Original question.
I want to retrieve all occurences of a string "aa". So in the search string
"aaaa", I have to find it three times (aa)aa, a(aa)a, aa(aa).

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

The following solution (previous post) works fine but ...
/(a(?=(a)))/g

Suppose I want to search for aaa or aab in the string aaabaabaa.

I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

Also, 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.

you could extend John's way, say:

print for "aaabaabaa" =~ /(?=(aa[ab]))/g

or some more regex's ways, like:

"aaabaabaa" =~ /(aa[ab])(?{print $1})(?!)/;

Let me show you how the above regex works:

(aa[ab]): get a match, and save the string into $1,
(?{print $1}): immediately print the matched string in $1
(?!) : matching failed, so $1 forcefully becomes non-matched.

According to the regex engine(the traditional NFA) behind Perl, the
regex will try another possiblity from the next trying-point until it
finds a match or finally fails. (?!) is the key that guarentees the
regex fails,(so the above matching expression will never success) and
the result is that the regex traverses all possiblities of the matching
cases and prints them out..

BTW. this is NOT a recommended way to solve your problem, but a way to
know something more about the Perl regex. you might find more useful
and interesting information in the book "Mastering Regular
Expressions"(strongly recommended).

Good luck,
Xicheng
 
C

Charles DeRykus

Xicheng said:
Peter said:
Hi,

Original question.
I want to retrieve all occurences of a string "aa". So in the search string
"aaaa", I have to find it three times (aa)aa, a(aa)a, aa(aa).

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

The following solution (previous post) works fine but ...
/(a(?=(a)))/g

Suppose I want to search for aaa or aab in the string aaabaabaa.

I thaught the following will do the job: a(?=a)[ab].
But is doesn't.

Also, 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.

you could extend John's way, say:

print for "aaabaabaa" =~ /(?=(aa[ab]))/g

or some more regex's ways, like:

"aaabaabaa" =~ /(aa[ab])(?{print $1})(?!)/;

Let me show you how the above regex works:

(aa[ab]): get a match, and save the string into $1,
(?{print $1}): immediately print the matched string in $1
(?!) : matching failed, so $1 forcefully becomes non-matched.

According to the regex engine(the traditional NFA) behind Perl, the
regex will try another possiblity from the next trying-point until it
finds a match or finally fails. (?!) is the key that guarentees the
regex fails,(so the above matching expression will never success) and
the result is that the regex traverses all possiblities of the matching
cases and prints them out..

BTW. this is NOT a recommended way to solve your problem, but a way to
know something more about the Perl regex. you might find more useful
and interesting information in the book "Mastering Regular
Expressions"(strongly recommended).

Just wanted to add thanks from the sidelines. I had never seen
the (?!) idiom and an example of its use.
 

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
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top