Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I write this regex?

Reply
Thread Tools

How do I write this regex?

 
 
spydox@gmail.com
Guest
Posts: n/a
 
      01-24-2006
This is NOT homework!

I want to comment out certain lines which immediately follow (as a
group) another line. I created a simple example:

$_ = 'ZBCAACBABE';

In English, for any group of A,B,or C's in any order, which follow a Z,
I want to preceed with ! . So in this example I want the regex to
produce:

Z! B! C! A! A! C! B! A! BE

I tried ?= but I don't see a way to make that work. I thought

s/(Z)(A|B|C)+/$1! $2/g;

might have a chance but it produced

'Z! BE'

I'm not sure I can explain that result.

If your suggestion involves a loop please dont make it, I know plenty
of ways to do that.

Thank-You

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-24-2006
(E-Mail Removed) wrote:
> This is NOT homework!


My, my. Mighty defensive, aren't we?

> I want to comment out certain lines which immediately follow (as a
> group) another line. I created a simple example:
>
> $_ = 'ZBCAACBABE';
>
> In English, for any group of A,B,or C's in any order, which follow a Z,
> I want to preceed with ! .


Try better English. An object for your transitive verb "preceed" would
be nice, for example.

> So in this example I want the regex to produce:
>
> Z! B! C! A! A! C! B! A! BE
>
> I tried ?= but I don't see a way to make that work. I thought
>
> s/(Z)(A|B|C)+/$1! $2/g;
>
> might have a chance but it produced
>
> 'Z! BE'
> I'm not sure I can explain that result.


Because your last parenthesized group has a + modifer. So when all is
said and done, $2 contains the *last* A, B, or C that it found. And
you replaced everything matched with $1, a !, and $2.

> If your suggestion involves a loop please dont make it, I know plenty
> of ways to do that.


Wait. You're imposing a requirement that you can't use a *loop*? And
you're claiming this isn't homework?

Please give me some real-life example of a situation in which a LOOP is
forbidden.

Paul Lalli

 
Reply With Quote
 
 
 
 
Todd W
Guest
Posts: n/a
 
      01-24-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> This is NOT homework!
>
> I want to comment out certain lines which immediately follow (as a
> group) another line. I created a simple example:
>
> $_ = 'ZBCAACBABE';
>
> In English, for any group of A,B,or C's in any order, which follow a Z,
> I want to preceed with ! . So in this example I want the regex to
> produce:
>
> Z! B! C! A! A! C! B! A! BE
>


<snip OP's code>

This may work for you:

$ cat lbstrrep.pl
use warnings;
use strict;

my $sequence = 'ZBCAACBABE';

$sequence =~ s|Z([ABC]+)|'Z! ' . join('! ', split(//, $1))|eg;

print( $sequence, "\n" );

$ perl lbstrrep.pl
Z! B! C! A! A! C! B! A! BE

The /e modifier evaluates the replacement side of the regex as perl code and
uses the result as the replacement string.

Enjoy,

trwww


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> This is NOT homework!
>
> I want to comment out certain lines which immediately follow (as a
> group) another line. I created a simple example:
>
> $_ = 'ZBCAACBABE';
>
> In English, for any group of A,B,or C's in any order, which follow a Z,
> I want to preceed with ! . So in this example I want the regex to
> produce:
>
> Z! B! C! A! A! C! B! A! BE
>
> I tried ?= but I don't see a way to make that work. I thought
>
> s/(Z)(A|B|C)+/$1! $2/g;
>
> might have a chance but it produced
>
> 'Z! BE'
>
> I'm not sure I can explain that result.
>
> If your suggestion involves a loop please dont make it, I know plenty
> of ways to do that.


$ perl -le'
$_ = q/ZBCAACBABE/;
print;
s{(?<=Z)([ABC]+)}{(my$x=$1)=~s<(?=.)><! >g;$x}eg;
print;
'
ZBCAACBABE
Z! B! C! A! A! C! B! A! BE



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Eric Bohlman
Guest
Posts: n/a
 
      01-25-2006
"Paul Lalli" <(E-Mail Removed)> wrote in news:1138134185.510690.251900
@z14g2000cwz.googlegroups.com:

> (E-Mail Removed) wrote:
>> If your suggestion involves a loop please dont make it, I know plenty
>> of ways to do that.

>
> Wait. You're imposing a requirement that you can't use a *loop*? And
> you're claiming this isn't homework?
>
> Please give me some real-life example of a situation in which a LOOP is
> forbidden.


I can: if the regex needs to be read in as data.
 
Reply With Quote
 
spydox@gmail.com
Guest
Posts: n/a
 
      01-25-2006
Paul - turn down the flames, I don't like doing other's homework here
and I wouldn't ask anyone else to do mine, that's all. No drama.. I'm
just stumped is all.

 
Reply With Quote
 
spydox@gmail.com
Guest
Posts: n/a
 
      01-25-2006
> I'm not sure I can explain that result.
>
>Because your last parenthesized group has a + modifer. So when all is
>said and done, $2 contains the *last* A, B, or C that it found. And
>you replaced everything matched with $1, a !, and $2.


Good explaination thanks! I sort of thought that the /g modifier would
interact differently with the + though. I was obviously wrong!

Spydox

 
Reply With Quote
 
spydox@gmail.com
Guest
Posts: n/a
 
      01-25-2006
I forbid it myself - as I said I know I can "loop" and solve this but I
prefer a regex over a loop *almost* everytime. 99% of the time I can
come up with a regex to solve anything that comes along, but this one
has me stumped!

Thanks everyone for the suggestions I'm trying them now. Particularly
your ideas John- that's sort of a intimidating regex but it sure seemed
to work! I'll have to cipher on that one a bit to comprehend it all



Spydox

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-25-2006
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> This is NOT homework!
>
> I want to comment out certain lines which immediately follow (as a
> group) another line. I created a simple example:
>
> $_ = 'ZBCAACBABE';
>
> In English, for any group of A,B,or C's in any order, which follow a Z,
> I want to preceed with ! . So in this example I want the regex to
> produce:


That's not a regex you want, it's a substitution operation. There's
a difference.

> Z! B! C! A! A! C! B! A! BE


Here is one way to do it:

s{Z([ABC]+)} {'Z' . join '', map "! $_", split //, $1}e;

or, same principle but using a nested substitution:

s{Z([ABC]+)} { my $x = $1; $x =~ s/(.)/! $1/g; "Z$x" }e;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-25-2006
Anno Siegel wrote:
> <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>This is NOT homework!
>>
>>I want to comment out certain lines which immediately follow (as a
>>group) another line. I created a simple example:
>>
>>$_ = 'ZBCAACBABE';
>>
>>In English, for any group of A,B,or C's in any order, which follow a Z,
>>I want to preceed with ! . So in this example I want the regex to
>>produce:

>
> That's not a regex you want, it's a substitution operation. There's
> a difference.
>
>> Z! B! C! A! A! C! B! A! BE

>
> Here is one way to do it:
>
> s{Z([ABC]+)} {'Z' . join '', map "! $_", split //, $1}e;


Or more simply:

s{(Z[ABC]+)} {join '! ', split //, $1}e;



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM
write a union object using write JasBascom C++ 1 02-24-2004 08:18 AM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM



Advertisments