私は、行の先頭にある4つのスペースをタブに置き換えることを検討していますが、テキストが存在するときはそれ以上のものを置き換えたくありません。スペースの字下げをタブで置き換える
読みやすさのために私の最初の正規表現/ {4}+/
または/[ ]{4}+/
がはっきりと働いていますが、明らかに4つのスペースで見つかったインスタンスは置き換えられます。
$string = ' this is some text --> <-- are these tabs or spaces?';
$string .= "\n and this is another line singly indented";
// I wrote 4 spaces, a tab, then 4 spaces here but unfortunately it will not display
$string .= "\n \t and this is third line with tabs and spaces";
$pattern = '/[ ]{4}+/';
$replace = "\t";
$new_str = preg_replace($pattern , $replace , $string);
echo '<pre>'. $new_str .'</pre>';
これは、式は、変換に関して完璧に動作しますが、----> <の間に4つのスペース、その事実のために与えられた正規表現を使用して、私がもともと持っていたものの一例でした----タブで置き換えられます。私はインデントを変更していないテキストを実際に探しています。
私の最善の努力は、これまでのラインの(^
)スタート([ ]{4}+
)最初の非空白ゴマ\s
$pattern = '/^[ ]{4}+.*?[;\s]*/m';
アップパターン(.*?[;\s]*
)何もしていインデントが失われているという事実は、私がここで紛失しているものを誰にでも理解できるようにすることができますか?
[編集]私がやろうとしています何を明確にするために
が、これは誰にも混乱し、なぜ、私は本当に理解していないタブのスペースからテキストのインデントの開始を変更です。 (上記$string
の値を使用して)できるだけ明確にすること
:
First line has 8 spaces at the start, some text with 4 spaces in the middle.
I am looking for 2 tabs at the start and no change to spaces in the text.
Second line has 4 spaces at the start.
I am looking to have only 1 tab at the start of the line.
Third line has 4 spaces, 1 tab and 4 spaces.
I am looking to have 3 tabs at the start of the line.
私は多分何かが欠けている。唯一のタブと4つのスペースの交換について質問です、またはあります – Niitaku
'preg_replace( '〜(?:^ | \ G)[] {4}〜m'、 '\ t"、$ s) 'を試してみてください、https://ideone.com/EzjRYCをご覧ください。 –
@WiktorStribiżewこれはまだそれの真ん中にタブで3行目に対処していません – Lucas