Pattern matching help! grep emails from file!

Discussion in 'Perl' started by danpres2k, Aug 22, 2003.

  1. danpres2k

    danpres2k Guest

    Hello, I have a file with email address at a lot of junk data. I want
    to get the email addresses out of that file so that each email address
    is stored at a new line. I am trying to do
    substitution:
    $filestring=<FILE>;
    $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;

    The file is like:
    "testin" , <testing>

    ""

    Expected output:





    Thanks guyz.
    danpres2k, Aug 22, 2003
    #1
    1. Advertising

  2. On Fri, 22 Aug 2003 10:46:22 -0400, danpres2k wrote:

    > Hello, I have a file with email address at a lot of junk data. I want to
    > get the email addresses out of that file so that each email address is
    > stored at a new line. I am trying to do substitution:
    > $filestring=<FILE>;
    > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;
    >
    > The file is like:
    > "testin" , <testing>
    > ""
    >
    > Expected output:
    >
    >
    >
    >
    >
    > Thanks guyz.


    Two things:

    1. Do you really want 2 newlines for each output?

    2. Since the first regex is matching the e-mail address and
    ONLY the e-mail address, you're actually telling the s/// to
    search the entire string for an e-mail address and substitute
    the e-mail address with itself, not, as you intend, to substitute
    the entire string with itself. You want to add a .* after the closing
    parenthesis, maybe.

    Instead of:
    > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;


    Try:
    > $filestring = s/(\w+\@\w+\.\w+).*/\n$1\n/;


    Or Possibly:
    > $filestring = s/.*(\w+\@\w+\.\w+).*/\n$1\n/;


    Untested, but I had a similar problem recently, and the
    principle is the same.

    Shawn
    Shawn Milochik, Aug 22, 2003
    #2
    1. Advertising

  3. danpres2k

    danpres2k Guest

    Shawn,

    Thanks for your help. But I couldn't use that as well. I am getting
    null value for $filestring when I am printing it:

    $filestring = <FILE>;
    $filestring = s/.*(\w+\@\w+\.\w+).*/$1/;
    print $filestring;

    Got any suggestion?
    Thanks.

    Shawn Milochik <> wrote in message news:<>...
    > On Fri, 22 Aug 2003 10:46:22 -0400, danpres2k wrote:
    >
    > > Hello, I have a file with email address at a lot of junk data. I want to
    > > get the email addresses out of that file so that each email address is
    > > stored at a new line. I am trying to do substitution:
    > > $filestring=<FILE>;
    > > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;
    > >
    > > The file is like:
    > > "testin" , <testing>
    > > ""
    > >
    > > Expected output:
    > >
    > >
    > >
    > >
    > >
    > > Thanks guyz.

    >
    > Two things:
    >
    > 1. Do you really want 2 newlines for each output?
    >
    > 2. Since the first regex is matching the e-mail address and
    > ONLY the e-mail address, you're actually telling the s/// to
    > search the entire string for an e-mail address and substitute
    > the e-mail address with itself, not, as you intend, to substitute
    > the entire string with itself. You want to add a .* after the closing
    > parenthesis, maybe.
    >
    > Instead of:
    > > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;

    >
    > Try:
    > > $filestring = s/(\w+\@\w+\.\w+).*/\n$1\n/;

    >
    > Or Possibly:
    > > $filestring = s/.*(\w+\@\w+\.\w+).*/\n$1\n/;

    >
    > Untested, but I had a similar problem recently, and the
    > principle is the same.
    >
    > Shawn
    danpres2k, Aug 22, 2003
    #3
  4. danpres2k

    danpres2k Guest

    Thanks again Shawn, It did work but only printed a part of the last
    email in the first line. how do i go about the newline chars in the
    $filestring? i am storing the string from the file handle in
    $filestring. is this correct?

    thanks.
    d

    Shawn Milochik <> wrote in message news:<>...
    > On Fri, 22 Aug 2003 16:57:00 -0400, danpres2k wrote:
    >
    > > Shawn,
    > >
    > > Thanks for your help. But I couldn't use that as well. I am getting null
    > > value for $filestring when I am printing it:
    > >
    > > $filestring = <FILE>;
    > > $filestring = s/.*(\w+\@\w+\.\w+).*/$1/; print $filestring;
    > >
    > > Got any suggestion?
    > > Thanks.
    > >
    > > Shawn Milochik <> wrote in message
    > > news:<>...
    > >> On Fri, 22 Aug 2003 10:46:22 -0400, danpres2k wrote:
    > >>
    > >> > Hello, I have a file with email address at a lot of junk data. I want
    > >> > to get the email addresses out of that file so that each email
    > >> > address is stored at a new line. I am trying to do
    > >> > substitution: $filestring=<FILE>;
    > >> > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;
    > >> >
    > >> > The file is like:
    > >> > "testin" , <testing>
    > >> > ""
    > >> >
    > >> > Expected output:
    > >> >
    > >> >
    > >> >
    > >> >
    > >> >
    > >> > Thanks guyz.
    > >>
    > >> Two things:
    > >>
    > >> 1. Do you really want 2 newlines for each output?
    > >>
    > >> 2. Since the first regex is matching the e-mail address and ONLY the
    > >> e-mail address, you're actually telling the s/// to search the entire
    > >> string for an e-mail address and substitute the e-mail address with
    > >> itself, not, as you intend, to substitute the entire string with
    > >> itself. You want to add a .* after the closing parenthesis, maybe.
    > >>
    > >> Instead of:
    > >> > $filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;
    > >>
    > >> Try:
    > >> > $filestring = s/(\w+\@\w+\.\w+).*/\n$1\n/;
    > >>
    > >> Or Possibly:
    > >> > $filestring = s/.*(\w+\@\w+\.\w+).*/\n$1\n/;
    > >>
    > >> Untested, but I had a similar problem recently, and the principle is
    > >> the same.
    > >>
    > >> Shawn

    >
    >
    > Yeah, just a typo. Replace
    > =
    > with:
    > =~
    >
    > I didn't catch that in the OP.
    >
    > Shawn
    danpres2k, Aug 25, 2003
    #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. Arowana Lin
    Replies:
    4
    Views:
    110
    Devin Mullins
    Dec 18, 2005
  2. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    230
    Marc Bissonnette
    Jan 13, 2004
  3. User
    Replies:
    5
    Views:
    457
    Tad McClellan
    Jun 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    225
    Xicheng Jia
    May 3, 2007
  5. nani
    Replies:
    2
    Views:
    160
    comp.llang.perl.moderated
    Mar 14, 2008
Loading...

Share This Page