2016-05-22 9 views
-2

私にはテキストがあり、このテキストの中の数字です。最後の数字については、数字の後に正確に同じ量の単語が必要です。 正規表現を書くことはできますか?私には分かりません。数字の後に正確な語数を見つけるにはどうすればいいですか

例:遠く離れ

は、単語の山の後ろに、遠くの国 VokaliaとConsonantiaから、3つの盲目のテキストがそこに住んでいます。彼らはセマンティクスの海岸にあるの4つのBookmarkgroveにライブで隔離されています。大きな 言語の海です。 Dudenという名前の小さな川が彼らの所で流れ、 はそれを必要な再生池に供給します。それはパラダイスの 国であり、文章の焙煎部分があなたの口に飛びます。
遠く

Vokalia
Consonantia
ブラインド

期待される結果は次のとおりです。

遠く

Vokalia
Consonantia
ブラインド

+1

私は質問を言い換える持っ – rock321987

+0

質問を言い換えてください。今はあなたのためにはっきりしていますか?私の英語はあまり良くありません。スレイ – Slash84

+0

@ Slash84これまでに試したことを示す必要があります。 – Tibrogargan

答えて

0

これがこの問題

$text = 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live 3 blind texts. Separated they live in 4 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. 
5 
far 
countries 
Vokalia 
Consonantia 
blind'; 

preg_match('/(\d+)(?!.*\d)(.+?)\Z/', $text, $words); 
var_dump(end($words)); 
0

ための私の解決策である私は後読みなしでそれをやってみましたが、私はできませんでした。

とにかく、ここで私はそれはしかし、単一の正規表現、ハードとより多くのCPUを消費して行うことができると確信している正規表現

(?<=\d)([^\d]+)$ 
+0

'\ K'トークンを使用して、以前に捕捉されたすべての文字が結果に存在しないようにリセットすることができます:' \ d \ K([^ \ d] +)$ ' – revo

-1

です。しかし、その心吹いアプローチせずに、私はこの1つで行くよ:

<?php 

$text = "Far far away, behind the word mountains, far from the countries Vokalia and 
Consonantia, there live 3 blind texts. Separated they live in 4 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. 
5 
far 
countries 
Vokalia 
Consonantia 
blind"; 

preg_match('/(\d+)([^\d]+)$/', $text, $match); 
var_dump(str_word_count($match[2]) == $match[1]); // bool(true) 
関連する問題