これらの条件に一致する正規表現を作成しようとしています:
- 最大 8000 文字 (「\r\n」を含む任意の文字)
- 最大 10 行 (\r\n で区切る)。
- 一致したテキストから最初の 4 行のみを抽出します。
それを行う良い方法を見つけることができません...:/
ありがとう!!
これらの条件に一致する正規表現を作成しようとしています:
それを行う良い方法を見つけることができません...:/
ありがとう!!
正規表現は必要なものではありません。特定の長さではなく、特定のパターンに一致させるために使用されます。データを astring
に保持している場合myString.length <= 8000
、文字カウントに必要なのは , だけです (もちろん、言語の正しい構文を使用します)。行数については、文字列内のシーケンスの数をカウントする必要があり\r\n
ます (繰り返し実行できます)。最初の 4 行を取得するには、4 行目を見つけて、\r\n
それより前のすべてをsubstring
メソッドで取得します。
この式は次のことを行います。
\A(?=.{0,8000}\Z)(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z)(?:^.*?[\r\n\Z]+){0,4}
これにはオプションが必要です:m
複数行、およびs
ドットはすべての文字に一致します
\A
文字列の先頭へのアンカー。このアンカーを使用すると、改行文字と改行文字を一致させるs
オプションを使用できます。.
(?=.{0,8000}\Z)
先を見越して、ゼロから 8000 文字の間にあることを検証します(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z)
先を見越して、改行で区切られた行が 10 行を超えないことを検証します(?:^.*?[\r\n\Z]+){0,4}
テキストの最初の 4 行に一致言語を指定しなかったため、この PHP の例を含めて、その動作とサンプル出力を示します。
入力テキスト
この入力テストは、改行で区切られた 8 行の文字列です。ここには 1779 文字しかありません。
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small
river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about
the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were
thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of
the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
she continued her way. On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word "and"
and the Little Blind Text should turn around and return to its own, safe country. But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe
and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasn’t been rewritten, then they are still using her.
コード
<?php
$sourcestring="your source string";
preg_match('/\A(?=.{0,8000}\Z)(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z)(?:^.*?[\r|\n\Z]+){0,4}/ims',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
マッチ
$matches Array:
(
[0] => Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small
river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about
the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were
thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of
)