Re: Regex being stupid
by davido (Cardinal) on Sep 19, 2013 at 03:06 UTC
|
I didn't bother reading past "\s" in your regex. I don't see a space character appearing anywhere before the "@" in your target string, so of course it can't match.
Why do you say the regex is being stupid, when it's just doing what you're telling it to do (ie, reject matches on strings that don't have a space character before a word character before an "@" character)?
| [reply] [Watch: Dir/Any] [d/l] |
Re: Regex being stupid
by Athanasius (Archbishop) on Sep 19, 2013 at 03:10 UTC
|
The code fragment shown has a number of problems. First, the string used to initialise $text contains the “@” symbol, which triggers interpolation within double quotes. This needs to be escaped, or else change to single quotes.
Second, the replacement part of a substitution cannot contain assertions such as \w+. Third, as davido says, the regex will fail to match because it requires a whitespace character (\s) before the first capture, but the given string does not contain any whitespace in that position.
Try this as a start:
#! perl
use strict;
use warnings;
my $text = 'pilar@delphoss.com type add (#090930-230011-907000 ur22122
+021 pi';
$text =~ /(\w+)@(\w+)\.com/;
print "$1\@$2\n";
Output:
12:59 >perl 724_SoPW.pl
pilar@delphoss
13:06 >
Hope that helps,
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: Regex being stupid
by CountZero (Bishop) on Sep 19, 2013 at 06:19 UTC
|
Have a look at Email::Find.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [Watch: Dir/Any] |
Re: Regex being stupid
by marinersk (Priest) on Sep 19, 2013 at 06:41 UTC
|
Hello again AI Cowboy,
[...] the variable is initialized through a large process in my code that I can't really release on here
And we would not want it here; part of making an effective question here, which encourages effective answers, is to only show a code snippet that demonstrates the issue.
This you have done; and please realize that people now picking nits at the sample code are still trying to help you, even though the specific problem you raise isn't there.
All part of the Monastery way. :-)
| [reply] [Watch: Dir/Any] |
Re: Regex being stupid
by boftx (Deacon) on Sep 19, 2013 at 07:07 UTC
|
I'm really too tired to take a crack at this (being awake only because of a roll-out that happened at 00:00) but if it can be guaranteed that the potential email address will always be the first element before a whitespace in a give input line then you might try this:
my ($email,$foo) = split(/\s/,$text,2);
You could then use any number of tools to see if $email is actually a potentially reasonable representation of an email address.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Regex being stupid
by dave_the_m (Monsignor) on Sep 19, 2013 at 07:21 UTC
|
In addition to what others have pointed out, the '@' in the regex needs escaping. @( is a valid variable name in perl (that happens to have zero elements in it normally).
Dave. | [reply] [Watch: Dir/Any] |
|
While it is true that @( is a valid variable name, it doesn’t interpolate:
@( = (42, 12); # valid statement
print "@("; # doesn’t print '4212' but '@('
print '@('; # same as above (obviously)
Thus, the @ in the regex doesn’t need escaping, strictly speaking. I’d still recommend escaping it, for clarity and maintainability. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Regex being stupid
by Laurent_R (Canon) on Sep 19, 2013 at 09:52 UTC
|
Assuming (just my guess) that what you wanted to do with the final (\w+) was to capture the end of the address, you could use this:
$text =~ s/(\w+)\@(\w+)\.([\w.]+)/$1@$2.$3/;
| [reply] [Watch: Dir/Any] [d/l] [select] |