0

これらの条件に一致する正規表現を作成しようとしています:

  • 最大 8000 文字 (「\r\n」を含む任意の文字)
  • 最大 10 行 (\r\n で区切る)。
  • 一致したテキストから最初の 4 行のみを抽出します。

それを行う良い方法を見つけることができません...:/

ありがとう!!

4

2 に答える 2

1

正規表現は必要なものではありません。特定の長さではなく、特定のパターンに一致させるために使用されます。データを astringに保持している場合myString.length <= 8000、文字カウントに必要なのは , だけです (もちろん、言語の正しい構文を使用します)。行数については、文字列内のシーケンスの数をカウントする必要があり\r\nます (繰り返し実行できます)。最初の 4 行を取得するには、4 行目を見つけて、\r\nそれより前のすべてをsubstringメソッドで取得します。

于 2013-06-20T21:44:30.717 に答える
1

説明

この式は次のことを行います。

  • 入力文字列が 0 ~ 8,000 文字であることを検証します
  • 改行で区切られたテキストが最大 10 行あることを検証します
  • 次に、改行で区切られた最初の 4 行のテキストをキャプチャします。

\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 コード例:

言語を指定しなかったため、この 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 

)
于 2013-06-21T03:24:25.783 に答える