s/// and /n question

s/// and /n question

am 21.07.2011 23:11:33 von Mike McClain

Given the following snippet of code could someone explain to me
why the linefeed gets included in $num?

mike@/deb40a:~/perl> perl -e'
$str = "asdfggfh 987321qwertyyy\n";
($num = $str) =~ s/^.*?(\d+).*$/$1/;
print $num;
' | hd
00000000 39 38 37 33 32 31 0a |987321.|
00000007

Thanks,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 21.07.2011 23:16:38 von Shawn H Corey

On 11-07-21 05:11 PM, Mike McClain wrote:
> Given the following snippet of code could someone explain to me
> why the linefeed gets included in $num?
>
> mike@/deb40a:~/perl> perl -e'
> $str = "asdfggfh 987321qwertyyy\n";
> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
> print $num;
> ' | hd
> 00000000 39 38 37 33 32 31 0a |987321.|
> 00000007
>
> Thanks,
> Mike

Try:

$ perl -e'
$str = "asdfggfh 987321qwertyyy\n";
($num = $str) =~ s/^.*?(\d+).*$/$1/s;
print $num;
' | hd

See `perldoc perlre` and search for /Modifiers/. Read the section on
the /s modifier.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 21.07.2011 23:28:25 von Jim Gibson

On 7/21/11 Thu Jul 21, 2011 2:11 PM, "Mike McClain"
scribbled:

> Given the following snippet of code could someone explain to me
> why the linefeed gets included in $num?
>
> mike@/deb40a:~/perl> perl -e'
> $str = "asdfggfh 987321qwertyyy\n";
> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
> print $num;
> ' | hd
> 00000000 39 38 37 33 32 31 0a |987321.|
> 00000007

Because the newline is not matched as part of /^.*?(\d+).*$/
Therefore, it is not replaced and is left in the string.

The .* does not gobble up the newline because . does not match newlines
unless the /s modifier is used. The $ is an anchor and does not match any
character.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 02:54:57 von Rob Dixon

On 21/07/2011 22:11, Mike McClain wrote:
> Given the following snippet of code could someone explain to me
> why the linefeed gets included in $num?
>
> mike@/deb40a:~/perl> perl -e'
> $str = "asdfggfh 987321qwertyyy\n";
> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
> print $num;
> ' | hd
> 00000000 39 38 37 33 32 31 0a |987321.|
> 00000007

Hey Mike

As others have noted, /./ without the /s modifier matches any character
but newline, so the regex matches only up to just before the "\n". But
your code would be better written as


use strict;
use warnings;

my $str = "asdfggfh 987321qwert99yyy\n";
my ($num) = $str =~ /(\d+)/;
print $num;


HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 03:06:55 von Shawn H Corey

On 11-07-21 08:54 PM, Rob Dixon wrote:
> On 21/07/2011 22:11, Mike McClain wrote:
>> Given the following snippet of code could someone explain to me
>> why the linefeed gets included in $num?
>>
>> mike@/deb40a:~/perl> perl -e'
>> $str = "asdfggfh 987321qwertyyy\n";
>> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
>> print $num;
>> ' | hd
>> 00000000 39 38 37 33 32 31 0a |987321.|
>> 00000007
>
> Hey Mike
>
> As others have noted, /./ without the /s modifier matches any character
> but newline, so the regex matches only up to just before the "\n". But
> your code would be better written as
>
>
> use strict;
> use warnings;
>
> my $str = "asdfggfh 987321qwert99yyy\n";
> my ($num) = $str =~ /(\d+)/;
> print $num;
>
>
> HTH,
>
> Rob
>

I think part of the confusion is also in what does $ match? According
to perlre, under "Regular Expressions",

$ Match the end of the line (or before newline at the end)

That means it can match the end of the string or a newline at the end of
the string.

Try this and see what it prints:

#!/usr/bin/env perl

use strict;
use warnings;

while( <> ){
print if /(\w+)$/
}
__END__


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 07:44:39 von rvtol+usenet

On 2011-07-22 03:06, Shawn H Corey wrote:

> According to perlre, under "Regular Expressions",
>
> $ Match the end of the line (or before newline at the end)
>
> That means it can match the end of the string or a newline at the end of
> the string.

Hello Shawn,

You are not reading exact enough. The $ can not match "a newline at the
end of the string". It matches a (relative) position, not a character.
This is also called "zero-width".

Further appreciate the word "line" (in stead of "buffer" or "string") in
its definition, and check the m-modifier.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 12:34:02 von derykus

On Jul 21, 6:06=A0pm, shawnhco...@gmail.com (Shawn H Corey) wrote:
> On 11-07-21 08:54 PM, Rob Dixon wrote:
> ...
> I think part of the confusion is also in what does $ match? =A0According
> to perlre, under "Regular Expressions",
>
> =A0 =A0 $ =A0 =A0 =A0 =A0Match the end of the line (or before newline at =
the end)
>
> That means it can match the end of the string or a newline at the end of
> the string.
>
> Try this and see what it prints:
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> while( <> ){
> =A0 =A0print if /(\w+)$/}
>
> __END__
>

Just entering a 'return' which will put a newline in
$_ won't print if the target regex is /(\w+)+$/.
Only [a-zA-Z_0-9] --or potentially more with
certain locale settings-- will. But not a newline
in any case.

Even /(.+)$/ won't match a single newline at the
end unless the regex '/s' modifier is used:

perl -E "$_ =3D qq{\n}; say qq{matched\n} if /(.)$/" <-- no

perl -E "$_ =3D qq{\n}; say qq{matched\n} if /(.)$/s" <---yes

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 16:59:54 von Rob Dixon

On 22/07/2011 15:25, Mike McClain wrote:
> On Fri, Jul 22, 2011 at 01:54:57AM +0100, Rob Dixon wrote:
>>
>> As others have noted, /./ without the /s modifier matches any character
>> but newline, so the regex matches only up to just before the "\n". But
>> your code would be better written as
>>
>> use strict;
>> use warnings;
>>
>> my $str = "asdfggfh 987321qwert99yyy\n";
>> my ($num) = $str =~ /(\d+)/;
>> print $num;
>
> Hi Rob,
> Thank you for your response and seeing how you moved the parens()
> taught me something

The point is that there is no need to write a substitution to delete
everything around the part of the string you are interested in. If you
put a match in list context (which is what the parentheses around $num
do) it will return all of the captured strings. So /(\d+)/ will find the
first sequence of digits in the string and assign it to $num.

> but I subscribe to the list and it's customary to keep replies on the
> list unless what you have to say shouldn't be broadcast.

I have been posting to beginners@perl.org for over ten years now, and am
aware of what is customary. I and most others do a 'Reply all' so that
the OP gets a personal response as well as revealing the dialogue to the
community. That is what I did in this case - you will see my reply on
the list if you look.

Rob



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 17:05:06 von Mike McClain

On Thu, Jul 21, 2011 at 02:11:33PM -0700, Mike McClain wrote:
> Given the following snippet of code could someone explain to me
> why the linefeed gets included in $num?
>
> mike@/deb40a:~/perl> perl -e'
> $str = "asdfggfh 987321qwertyyy\n";
> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
> print $num;
> ' | hd

My thanks to Shawn, Jim, Rob & Dr. Rudd for their answers.
Now I not only know why I was getting the '\n' but 2 ways to avoid it.

($num = $str) =~ s/^.*?(\d+).*$/$1/s;
or
($num) = $str =~ /^.*?(\d+).*$/;

Danke,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 17:10:22 von Rob Dixon

On 22/07/2011 16:05, Mike McClain wrote:
> On Thu, Jul 21, 2011 at 02:11:33PM -0700, Mike McClain wrote:
>>
>> Given the following snippet of code could someone explain to me
>> why the linefeed gets included in $num?
>>
>> mike@/deb40a:~/perl> perl -e'
>> $str = "asdfggfh 987321qwertyyy\n";
>> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
>> print $num;
>> ' | hd
>
> My thanks to Shawn, Jim, Rob& Dr. Rudd for their answers.
> Now I not only know why I was getting the '\n' but 2 ways to avoid it.
>
> ($num = $str) =~ s/^.*?(\d+).*$/$1/s;
> or
> ($num) = $str =~ /^.*?(\d+).*$/;

Again, there is no need for all that noise in the regex.

my ($num) = $str =~ /(\d+)/;

is fine.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: s/// and /n question

am 22.07.2011 20:40:56 von JPH

Are you familiar with chomp( $str ) ? Often you will want to use chomp to get rid of trailing newlines on your input lines.

On 07/21/2011 11:11 PM, Mike McClain wrote:
> Given the following snippet of code could someone explain to me
> why the linefeed gets included in $num?
>
> mike@/deb40a:~/perl> perl -e'
> $str = "asdfggfh 987321qwertyyy\n";
> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
> print $num;
> ' | hd
> 00000000 39 38 37 33 32 31 0a |987321.|
> 00000007
>
> Thanks,
> Mike

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/