questions about RE!

F

Flamingo

hey all,

I'm a perl novice. now I'm studying regular expression. I have 2
questions :

1. Your current program should count lines of a file which contain a
certain string. Modify it so that it counts lines with double letters
(or any other double character). Modify it again so that these double
letters appear also in parentheses. For example your program would
produce a line like this among others:

023 Amp, James Wa(tt), Bob Transformer, etc. These pion(ee)rs
conducted many

Try to get it so that all pairs of letters are in parentheses, not
just the first pair on each line.


2. For a slightly more interesting program you might like to try the
following. Suppose your program is called countlines. Then you would
call it with

../countlines

However, if you call it with several arguments, as in

../countlines first second etc

then those arguments are stored in the array @ARGV. In the above
example we have $ARGV[0] is first and $ARGV[1] is second and $ARGV[2]
is etc. Modify your program so that it accepts one argument and counts
only those lines with that string. It should also put occurrences of
this string in paretheses. So

../countlines the

will output something like this line among others:

019 But (the) greatest Electrical Pioneer of (the)m all was Thomas
Edison, who

can anyone give me some solutions. thanks a lot.
 
A

A. Sinan Unur

hey all,

I'm a perl novice. now I'm studying regular expression. I have 2
questions :

1. Your current program should count lines
....

can anyone give me some solutions. thanks a lot.

It is *your* homework. Do it yourself, or drop the class!

*PLONK*

Sinan
 
P

Peter Wyzl

Flamingo said:
hey all,

I'm a perl novice. now I'm studying regular expression. I have 2
questions :

1. Your current program should count lines of a file which contain a
certain string. Modify it so that it counts lines with double letters
(or any other double character). Modify it again so that these double
letters appear also in parentheses. For example your program would
produce a line like this among others:

023 Amp, James Wa(tt), Bob Transformer, etc. These pion(ee)rs
conducted many

Try to get it so that all pairs of letters are in parentheses, not
just the first pair on each line.


2. For a slightly more interesting program you might like to try the
following. Suppose your program is called countlines. Then you would
call it with

./countlines

However, if you call it with several arguments, as in

./countlines first second etc

then those arguments are stored in the array @ARGV. In the above
example we have $ARGV[0] is first and $ARGV[1] is second and $ARGV[2]
is etc. Modify your program so that it accepts one argument and counts
only those lines with that string. It should also put occurrences of
this string in paretheses. So

./countlines the

will output something like this line among others:

019 But (the) greatest Electrical Pioneer of (the)m all was Thomas
Edison, who

can anyone give me some solutions. thanks a lot.

1) s/(..)/(\1)/ig

2) What have you done so far?

P
 
J

John W. Krahn

Peter said:
1) s/(..)/(\1)/ig

You probably meant:

s/((.)\2)/($1)/g

Or:

s/(.)\1/($1$1)/g

There are no letters in the pattern for the /i option to ignore case on and \1
should only be used *inside* a regular expression.



John
 

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
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top