regex - Perl one-liner to convert first character uppercase - logic understanding -


in following perl command lines, trying turn first , second char uppercase

echo pet | perl -pe 's/^(.{0})(.)/$1\u$2/'    # ans: pet echo pet | perl -pe 's/^(.{1})(.)/$1\u$2/'    # ans: pet 

but not understand syntax (.{0})(.) , (.{1})(.)

could clarify how works?

however, found above can achieved following syntax:

echo pet | perl -pe 's/(\w)/\u$1\e/'   # ans: pet echo pet | perl -pe 's/(\w)(\w)/$1\u$2/' # ans: pet 

the reference when placed between \u , \e converted uppercase

the difference between:

echo pet | perl -pe 's/^(.{0})(.)/$1\u$2/'    # ans: pet echo pet | perl -pe 's/^(.{1})(.)/$1\u$2/'    # ans: pet 

is nothing matched in first capture group in first case, whereas p captured in first group in second case.

a shorter equivalent of first case be:

$ echo pet | perl -pe 's/^(.)/\u$1/' pet 

additionally, following should clarify it:

$ echo pet | perl -pe 's/^(.{0})(.)/$1\u$2$2/' ppet 

(the second backreference printed twice, , produces 2 ps.)


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -