Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Removing data from a string with Regex

by jwest (Friar)
on Nov 15, 2001 at 02:14 UTC ( #125441=note: print w/ replies, xml ) Need Help??


in reply to Removing data from a string with Regex

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


Comment on Re: Removing data from a string with Regex
Select or Download Code
Re: Re: Removing data from a string with Regex
by pike (Monk) on Nov 15, 2001 at 13:46 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://125441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2014-04-16 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls