Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   noob question: Trying to extract part of a string in a variable to another variable (http://www.velocityreviews.com/forums/t886122-noob-question-trying-to-extract-part-of-a-string-in-a-variable-to-another-variable.html)

cayenne 04-25-2004 03:59 PM

noob question: Trying to extract part of a string in a variable to another variable
 
Hello all,
I'm a perl noob...and just can't quite figure out how to do something
that should be pretty simple.

Here's an example.

I have $mail_address = 'fred jones <fred_jones@somewhere.com>'

I want to use regular expressions to just parse out the userid here of
fred_jones

I'm trying things like this:

$mail_address =~ /\w+@/;

But, doesn't seem to work. I'm a little hazy on exactly how the =~
works...through examples I've successfully used it for substitutions
like x =~ s/tom/joe/g; but, I'm just wanting to match a regular
expression and extract it to the variable...or even to another
variable leaving $mail_address unchanged.

I've looked in books at the substr() function, but, I don't know how
to use regular expressions to find the offset point, etc.

Can someone give me an example...or pointers to a good reference on
this type of thing?

Thanks in advance,

chilecayenne

gnari 04-25-2004 04:19 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
"cayenne" <chilecayenne@yahoo.com> wrote in message
news:2deb3d1.0404250759.7676bbb5@posting.google.co m...
> I'm trying things like this:
>
> $mail_address =~ /\w+@/;
>
> But, doesn't seem to work.


'doesn't seem to work' does not tell us anything
except that you expected it to do something other
than what it does. many of us have negligent PSI
powers, so it helps us not a lot.

on the other hand, maybe what you want is:

my ($id)= $mail_address =~ /(\w+)@/;

>
> I've looked in books at the substr() function, but, I don't know how
> to use regular expressions to find the offset point, etc.


>
> Can someone give me an example...or pointers to a good reference on
> this type of thing?



take a look at the perl documentation:
perldoc perlop
perldoc perlre

gnari




Jürgen Exner 04-25-2004 04:27 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
cayenne wrote:
> Here's an example.
>
> I have $mail_address = 'fred jones <fred_jones@somewhere.com>'
>
> I want to use regular expressions to just parse out the userid here of
> fred_jones
>
> I'm trying things like this:
>
> $mail_address =~ /\w+@/;
>
> But, doesn't seem to work.


Please define "doesn't seem to work". What exactly do you expect that
statement to do and what do you observe instead? Like, what do you mean by
"parse out"? Do you want to remove the userid from the string? Or do you
want to capture the userid in a different variable?

> I'm a little hazy on exactly how the =~
> works...


It is the binding operator. If used the substitute or match will be applied
to the variable on it's left side instead of to the default $_.

> through examples I've successfully used it for substitutions
> like x =~ s/tom/joe/g; but, I'm just wanting to match a regular
> expression and extract it to the variable...or even to another
> variable leaving $mail_address unchanged.


Well, Perl regular expressions do that automatically. Just use grouping:

my $mail_address = 'fred jones <fred_jones@somewhere.com>';
$mail_address =~ /(\w+)@/;
print $1;

Further details "perldoc perlretut" or for the advanced part "perldoc
perlre"

However, I hope you are aware that '\w' does not even begin to cover the
full set of possible email aliases.
Please see "perldoc -q valid", third paragraph for further information.

> I've looked in books at the substr() function, but, I don't know how
> to use regular expressions to find the offset point, etc.


You don't. You would use index() to find the position of a character or
string in a text.

jue



Bob Walton 04-25-2004 04:32 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
cayenne wrote:

....


> I have $mail_address = 'fred jones <fred_jones@somewhere.com>'
>
> I want to use regular expressions to just parse out the userid here of
> fred_jones

....


> Can someone give me an example...or pointers to a good reference on
> this type of thing?

....
> chilecayenne
>


Try:

my($userid)=$mail_address=~/(\w+)@/;

References:

perldoc perlre
perldoc perlretut
perldoc perlop

The books: "Learning Perl (3rd edition)", "Programming Perl (3rd
edition)" and "Mastering Regular Expressions (2nd edition)".

Online: learn.perl.org, www.perl.com, www.perldoc.com

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


Milo Minderbinder 04-25-2004 04:38 PM

Re: noob question: Trying to extract part of a string in a variableto another variable
 
cayenne schrieb:
> Hello all,
> I'm a perl noob...and just can't quite figure out how to do something
> that should be pretty simple.
>
> Here's an example.
>
> I have $mail_address = 'fred jones <fred_jones@somewhere.com>'
>
> I want to use regular expressions to just parse out the userid here of
> fred_jones
>
> I'm trying things like this:
>
> $mail_address =~ /\w+@/;
>
> But, doesn't seem to work. I'm a little hazy on exactly how the =~
> works...through examples I've successfully used it for substitutions
> like x =~ s/tom/joe/g; but, I'm just wanting to match a regular
> expression and extract it to the variable...or even to another
> variable leaving $mail_address unchanged.
>
> I've looked in books at the substr() function, but, I don't know how
> to use regular expressions to find the offset point, etc.
>
> Can someone give me an example...or pointers to a good reference on
> this type of thing?
>
> Thanks in advance,
>
> chilecayenne


Hi,

you have to mark the part you want to get.

$mail_address =~ m/(\w+?)@/;
$name = $1;

Take brackets to mark what you want. You will find the result in $1. If
you specify more then one part, you will find the second hit in $2. The
questionsign within the brackets avoids, that you get as much as
possible into your result (if there two or more @).
Other way to get results is:

my @result = $mail_address =~ m/(\w+?)@/;

In $result[0] you will find then name.

Milo




Web Surfer 04-25-2004 04:47 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
[This followup was posted to comp.lang.perl.misc]

In article <2deb3d1.0404250759.7676bbb5@posting.google.com> ,
chilecayenne@yahoo.com says...
> Hello all,
> I'm a perl noob...and just can't quite figure out how to do something
> that should be pretty simple.
>
> Here's an example.
>
> I have $mail_address = 'fred jones <fred_jones@somewhere.com>'
>
> I want to use regular expressions to just parse out the userid here of
> fred_jones
>
> I'm trying things like this:
>
> $mail_address =~ /\w+@/;
>
> But, doesn't seem to work. I'm a little hazy on exactly how the =~
> works...through examples I've successfully used it for substitutions
> like x =~ s/tom/joe/g; but, I'm just wanting to match a regular
> expression and extract it to the variable...or even to another
> variable leaving $mail_address unchanged.
>
> I've looked in books at the substr() function, but, I don't know how
> to use regular expressions to find the offset point, etc.
>
> Can someone give me an example...or pointers to a good reference on
> this type of thing?
>
> Thanks in advance,
>
> chilecayenne
>



#!/usr/bin/perl -w

use strict;

my ( $mail_address , $userid );

$mail_address = 'fred jones <fred_jones@somewhere.com>';
$mail_address =~ /(\w+)@/;

$userid = $1;

print "Userid = [$userid]\n";

exit 0;

Tad McClellan 04-25-2004 05:59 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
Jürgen Exner <jurgenex@hotmail.com> wrote:

> Just use grouping:
>
> my $mail_address = 'fred jones <fred_jones@somewhere.com>';
> $mail_address =~ /(\w+)@/;
> print $1;



But don't use it like that!

You should never use the dollar-digit variables without first ensuring
that the match *succeeded*.

if ( $mail_address =~ /(\w+)@/ ) {
print $1;
}


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Tad McClellan 04-25-2004 06:01 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
Milo Minderbinder <noMail@fmail.com> wrote:

[ snip full-quote, please don't do that]

> you have to mark the part you want to get.
>
> $mail_address =~ m/(\w+?)@/;
> $name = $1;
>
> Take brackets to mark what you want. You will find the result in $1.

^^^^
^^^^

No, you *might* find the result in $1.

If you've tested that the match *succeeded*,
_then_ you will find the result in $1.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Tad McClellan 04-25-2004 06:02 PM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
Web Surfer <raisin@delete-this-trash.mts.net> wrote:

> $mail_address =~ /(\w+)@/;
> $userid = $1;



What is with this epidemic of teaching the WRONG way in this thread?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Sherm Pendley 04-26-2004 06:46 AM

Re: noob question: Trying to extract part of a string in a variable to another variable
 
Robin wrote:

> Regular expressions are not the right way to find the offset unless you
> want to use $1 an $2 and $3...etc, and then use index, it still isn't an
> optimal way to find the offset point.


Darn right it's not. If your pattern has subexpressions, then on a match the
offset of each subexpression appears in the @- array. That is, the offset
of $1 is in $-[0], $2 is in $-[1], and so forth.

Note that offsets, no matter how they're found, are irrelevant to the
original question anyway. All he wanted was the value of the matched
substring, not its position. He was thinking he might need to offset to get
the substring, but he was barking in the wrong forest with that idea.

So tell me Robin, when are you going to stop posting nonsense answers to
questions you don't understand?

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


All times are GMT. The time now is 10:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.