Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
Replies are listed 'Best First'.
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 contemplating the Monastery: (7)
As of 2015-07-08 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls