Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Newbe looking for some help. I have the following snipet of text from an email. I would like to use regex to return the complete line of the address to a variable. "Service Location: 1234 n street ave, Some City, ST, 99999, US" White space follows the address line in the email. Thanks in advance.

Replies are listed 'Best First'.
Re: regex help
by kcott (Archbishop) on Oct 02, 2013 at 16:21 UTC

    Put code and data within <code>...</code> tags so we can see what it really looks like. See "Writeup Formatting Tips" for further details. Furthermore, don't show part of the data and a narrative explaining other parts: show a representative sample of your data in its entirety. See "How do I post a question effectively?" for more details on this.

    Guessing somewhat as to the real formatting, this code is possibly close to what you need:

    #!/usr/bin/env perl use strict; use warnings; my $text = <<'EOD'; Service Location: 1234 n street ave, Some City, ST, 99999, US EOD $text =~ /Service Location:\s+(.*\S)\s*$/m; print "$1\n";

    Output:

    1234 n street ave, Some City, ST, 99999, US

    -- Ken

Re: regex help
by davido (Cardinal) on Oct 02, 2013 at 16:00 UTC

    By whitespace, do you mean newline, or do you mean that it could possibly be "99999, US irrelevant"?

    Assuming newline, since you mentioned "complete line":

    if( $string =~ m/Service Location:\s+(.+)$/ ) { $address = $1; }

    You should read perlretut and perlrequick, for starters.


    Dave

Re: regex help
by Anonymous Monk on Oct 02, 2013 at 15:58 UTC

    The message did not save the formating correctly. After "Service Location" is looks like there are two carrige returns.

    Again thanks for the help.

      Wrap your sample data in <code></code> tags, and post a sample that contains two records, so we can see how they're separated. Bad formatting on sample data will lead to incorrect answers.


      Dave