2

php regexpを簡単な方法で使用して、文字列を変更して、単語の後に続くピリオドの後にスペースを追加することはできますが、1.00などの数字の前後のピリオドの後にはスペースを追加できませんか?NYなどの1文字の略語も無視する必要があります

String.Looks like this.With an amount of 1.00 and references N.Y.

に変更する必要があります...

String. Looks like this. With an amount of 1.00 and references N.Y.

もちろん、これにより文字列内の複数のインスタンスが可能になります...

4

2 に答える 2

5
$text = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $text);
于 2012-08-26T03:25:15.923 に答える
1

次の正規表現を使用できます。

/((?<=[A-Za-z0-9])\.(?=[A-Za-z]{2})|(?<=[A-Za-z]{2})\.(?=[A-Za-z0-9]))/

そして、置換文字列は'. '.

上記の正規表現では、ピリオドの片側に少なくとも 1 つのアルファベット文字が必要であり.、反対側に英数字が必要であることに注意してください。

テスト文字列:

"String.Looks like this.With an amount of 1.00 and references N.Y.Mr.NoOne.30% replaced.no 50.Don't know."

出力:

"String. Looks like this. With an amount of 1.00 and references N.Y. Mr. NoOne. 30% replaced. no 50. Don't know."
于 2012-08-26T03:31:04.843 に答える