2016-07-19 5 views
1

文字列のすべてを取り除いて<span class="notranslate">*any text*</span>の文字列を取り込もうとしています(HTMLを解析する必要はありません。私は他のタグを保持したいので、タグは削除するために正確に一致する必要があります)。与えられた文字列に少なくとも1つのタグがあり、上限はありません(2つ以上のものは珍しいでしょうが)regexを使用して特定の(繰り返しの可能性がある)パターン以外のすべてをキャプチャする

私の究極の目標は、変数名と変数名その値に置き換えられました(変数を自分で置き換えることはできませんが、私はそのdbにアクセスできません)。これらの変数は、私が言及したスパンタグで常に囲まれます。私は自分のタグが「翻訳しない」と言っていることを知っていますが、これは前翻訳であるため、他のテキストはすべて同じになります。例えば

、これらは私の二つの入力テキストであれば:

Dear <span class="notranslate">$customer</span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate">$article431</span> and let me know if that fixes your problem.

Dear <span class="notranslate">John Doe</span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate">http://url.for.help/article</span> and let me know if that fixes your problem.

私は正規表現を返すようにしたい:
Dear , I am sorry that you are having trouble logging in. Please follow the instructions at this URL and let me know if that fixes your problem.
OR
Dear <span class="notranslate"></span>, I am sorry that you are having trouble logging in. Please follow the instructions at this URL <span class="notranslate"></span> and let me know if that fixes your problem.
両方のために、私は簡単にString.Equals()を実行し、それらが等しいかどうかを調べることができます。 (変数を置き換えた複数のテキストと入力変数を比較して一致するものを見つける必要があります)

文字列に "not notllate"があるかどうかを簡単に知ることができました"セクション:(<span class="notranslate">(.+?)</span>)、これは私が比較の前にセクションを取り除く必要があるかどうかを決定する方法です。しかし、私は上記の(私は非常に似たような)仕事に多くの問題を抱えています。

私はExpressoとregexstorm.netをテストに使用しており、(?:(.+?)(?:<span class=\"notranslate\">(?:.+?)</span>))のさまざまなバリエーションを使用していますが、他のSO質問のアイデアを使用していますが、それらのすべてがわかりません。たとえば、Expressoではほとんど機能するようですが、最後のスパンタグの後には終了テキストを取得することはできません。スパンタグをオプションにするか、最後に別の(。+?)を追加しようとすると、何も取得できません。先読みを使ってみましたが、後でタグ+内部テキストを後で取得してしまいます。

+1

あなたは置き換える操作を行うことはできません。

string data = "Dear <span class=\"notranslate\">$customer</span>, I am sorry that you\r\n are havin" + "g trouble logging in. Please follow the instructions at this\r\n URL <span class=" + "\"notranslate\">$article431</span> and let me know if\r\n that fixes your problem."; string pattern = @"(?<Words>[^<]+)(?<Ignore><[^>]+>[^>]+>)?"; Regex.Matches(data, pattern) .OfType<Match>() .Select(mt => mt.Groups["Words"].Value) .Aggregate((sentance, words) => sentance + words); 

結果は、元のキャリッジリターンとラインを持っている文字列は、実際にあなたの例ではフィードのですか?あなたの最初の正規表現を使用し、各一致を空の文字列で置き換えます。 – 4castle

+0

親愛なる神。うん、それはうまくいく。私はregexesを初めて使用することに夢中になり、トンネルビジョンを開発しました。ありがとう。 – violaceous

+0

Phew!それはあなたが思っていたよりも簡単だと分かったとき、いつもいい感じです。うれしいです:) – 4castle

答えて

0

これはすべてをキャプチャし、無視される一致したHTMLタグを処理します。

Dear , I am sorry that you 
    are having trouble logging in. Please follow the instructions at this 
    URL and let me know if 
    that fixes your problem. 
関連する問題