questions about RE!

Discussion in 'Perl Misc' started by Flamingo, Feb 9, 2007.

  1. Flamingo

    Flamingo Guest

    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.
     
    Flamingo, Feb 9, 2007
    #1
    1. Advertising

  2. "Flamingo" <> wrote in news:1171056082.706119.63850
    @a34g2000cwb.googlegroups.com:

    > 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
     
    A. Sinan Unur, Feb 9, 2007
    #2
    1. Advertising

  3. Flamingo

    Peter Wyzl Guest

    "Flamingo" <> wrote in message
    news:...
    > 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
     
    Peter Wyzl, Feb 10, 2007
    #3
  4. Peter Wyzl wrote:
    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Feb 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    563
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,123
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    557
  4. Vijay
    Replies:
    2
    Views:
    596
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    281
    reema
    Aug 26, 2008
Loading...

Share This Page