Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Removing data from a string with Regex

by IPstacks (Novice)
on Nov 15, 2001 at 02:03 UTC ( [id://125435]=perlquestion: print w/replies, xml ) Need Help??

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

I have the following script:
open FILE, $filename || die $!; while (<FILE>) { #search for --from if (/--from/) { $from = $_; $from =~ s/^--from\t//g; $from =~ s/\t\\//g; $from =~ s/@//g; print $from; }
The original value of $from is "--from me@mydomain.com \"
The problem that I am having is the part where I read in $from =~ s/@//g; I want to remove everything that comes after the @ in the string.
i.e.. I have me@mydomain.com and I just want $from to equal me and remove the @mydomain.com
Since this is my first time using Regular Expressions, I'm a little puzzled by what I need to do to get this to work, so, any help is greatly appreciated!

Replies are listed 'Best First'.
Re: Removing data from a string with Regex
by jwest (Friar) on Nov 15, 2001 at 02:14 UTC
    Instead of using substitution, you might try something like this:

    $from =~ /^([^@]+)@/; $from = $1;

    The first line will take all the characters starting from the beginning of $from that aren't '@' and capture them to the special variable $1. Then, reassign $from to the value of $1.

    To do this with substitution, you could try:

    $from =~ s/@.*//;
    which would substitute the '@' and anything that followed with nothing.

    Hope this helps!

    --jwest


    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
      Taking this approach one step further, the original loop could be simplified to:

      while (<FILE>) { my ($name) = /^--from\s*([^@]+)/; print ($name) if defined $name; }

      pike

        That regex would allow --from @something.com to pass and assign ' ' to $name. It would likely be better to use:/^--from\s*([^@\s]+)/ which fails for --from @something.com and could simplify the code further.

        while ( <FILE> ) { print $1 if /^--from\s*([^@\s]+)/; } or: print $1 if /^--from\s*([^@\s]+)/ while <FILE>;



        HTH,
        Charles K. Clarkson
Re: Removing data from a string with Regex
by c (Hermit) on Nov 15, 2001 at 02:16 UTC
    you may want to look at this to look over the basics of regular expressions. basically, i think you're wanting to match everything after the @. so you may want to look through perlre and look for what matches any single character and how to match it one or more times.

    the other option, you might want to take on this one is splitting the string based on the @ and grabbing the first instance within the split. something like

    $from = (split(/\@/, $from))[0];

    humbly -c

Re: Removing data from a string with Regex
by jclovs (Sexton) on Nov 15, 2001 at 02:29 UTC
    In true TMTOWTDI style you could also use that which is below, although it is lacking a bit in the optimization.
    $from =~ /@/; $from = $`;
    jclovs
      You have a common precedence issue. Change the following line.
      open FILE, $filename || die $!;
      to
      open FILE, $filename or die $!;
      or if you really want to use ||
      open(FILE,$filename) || die $!;
      also it wouldn't hurt to be more verbose:
      die "Could not open $filename: $!\n";
      UPDATE: The usage of the \g modifier is for multiple matching,substituting,etc. you don't need it if you know what you are searching for only occurs once as is the case:

      The original value of $from is "--from me@mydomain.com \"

      EXAMPLE:
      $str ='abcd'; $str =~s/a//g; # \g unnecessary ## $str = 'abacad'; $str =~s/a//g; # more appropriate
      It wouldn't hurt to also have a look at perlop

      -tengo mas que aprender
Re: Removing data from a string with Regex
by scain (Curate) on Nov 15, 2001 at 02:13 UTC
    What you need is $from =~ s/@.+//g;; the ".+" matches everything from @ to the end of the string.

    Scott

    Minor update: Just to be clear, this is probably not the best way to do this, it just happens to work. Specifically, the .+ matches upto but not including the line feed that is at the end of the string, since it came along to $from from $_. In general, you would probably want to chomp $_ and then explicitly add a "\n" to your print statement, so that it is clear what you are doing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://125435]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-20 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found