Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp question: ^ vs \A 1

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
0
0
US
Hi, Experts,

What is the exact difference between ^ & \A?

According to perl books, ^ matches the beginning of a line and \A matches the beginning of a string. Wouldn't they mean the same?

Could someone show me the difference through examples?

In addition, how to use regexp to change the first char of a string into upper case? I know how to do it using substr. But I'd like to know how to do it using regexp.

Many thanks.
 
Hi

A string has a single beginning but it may contain more then on lines, in which case each line has its beginning :
Code:
[blue]  DB<1>[/blue] $str="Hello World\nHello Perl Hello Tek-Tips";

[blue]  DB<2>[/blue] print $str=~m/\A(Hello \w+)/gm; # Hello at the beginning of string
Hello World

[blue]  DB<3>[/blue] print $str=~m/^(Hello \w+)/gm; # Hello at the beginning of lines
Hello WorldHello Perl
Additionally this is bad idea :
Code:
[blue]  DB<1>[/blue] $str='whn';

[blue]  DB<2>[/blue] $str=~s/(.)/\u\1/;

[blue]  DB<3>[/blue] print $str;
Whn
Better do it like this :
Code:
[blue]  DB<1>[/blue] $str='whn';

[blue]  DB<2>[/blue] $str=ucfirst $str;

[blue]  DB<3>[/blue] print $str;
Whn

Feherke.
 
Thank you so much, Feherke.

Your answer helps a lot.

BTW, I did not know ucfirst before. I suppose there is uclast, too? I am gong to check it now.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top