Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Replace multiple tabs with spaces at start of a string

by choroba (Cardinal)
on May 16, 2017 at 22:52 UTC ( [id://1190411]=note: print w/replies, xml ) Need Help??


in reply to Replace multiple tabs with spaces at start of a string

Why can't you replace the tabs directly?
s/^(\t+)/' ' x (4 * length $1)/e;

The number of spaces will be four times the number of tabs.

Or, without /e :

$s =~ s/(^|\G)\t/ /g;

Replace a tab with 4 spaces if it's at the start of the string or after a previously replaced tab.

Update: The latter could be simplified to

$s =~ s/\G\t/ /g;

as the "last match" is initially set to the beginning of the string.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Replace multiple tabs with spaces at start of a string
by Eily (Monsignor) on May 17, 2017 at 13:10 UTC

    ++

    With the /m modifier, the first solution also works on multiline input: print $_ and print s/(^|\G)\t/____/mgr for "\tHello\t\n\t\tWorld\t!\n\t!\n";

Re^2: Replace multiple tabs with spaces at start of a string
by mavericknik (Sexton) on May 17, 2017 at 07:11 UTC
    Thank you very much! I was not aware of \G in perl, I will look into it.
Re^2: Replace multiple tabs with spaces at start of a string
by Anonymous Monk on May 17, 2017 at 20:15 UTC
    what is the meaning of the \G there??
      Anchor where to start for next match.

      The \G anchor matches at the point where the previous//g match left off. \G allows us to easily do context-sensitive matching... perlretut

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-29 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found