2017-08-17 9 views
-1

新しい `ですべてを置き換える、私は私のHTMLに次のテキストを持ちたいと何か他のもの抽出IDと正規表現に例HTML`

例HTMLと交換したいと思います:

{{Object id='foo'}} 

このような変数にIDを抽出します。

string strId = "foo"; 

これまでのところ私は例HTMLをキャプチャします次の正規表現コードを持っています

string strStart = "Object"; 
string strFind = "{{(" + strStart + ".*?)}}"; 
Regex regExp = new Regex(strFind, RegexOptions.IgnoreCase); 

Match matchRegExp = regExp.Match(html); 

while (matchRegExp.Success) 
{ 

    //At this point, I have this variable: 
    //{{Object id='foo'}} 

    //I can find the id='foo' (see below) 
    //but not sure how to extract 'foo' and use it 

    string strFindInner = "id='(.*?)'"; //"{{Slider"; 
    Regex regExpInner = new Regex(strFindInner, RegexOptions.IgnoreCase); 
    Match matchRegExpInner = regExpInner.Match(matchRegExp.Value.ToString()); 

    //Do something with 'foo' 

    matchRegExp = matchRegExp.NextMatch(); 
} 

私は、これは簡単な解決策になるかもしれません理解して、私は正規表現についてより多くの知識を得ることを期待していますが、より重要なのは、私はより効率的に、このクリーナーに近づくと方法についての提案を受け入れることを望んでいます。

はありがとう

編集:c# regex replace

+0

停止!見て聞く!毎日、正規表現を使ってHtmlを解析するという素晴らしいアイデアの中で、何人かが目を覚まします。 Xmlパーサーよりも優れたHtmlパース。 あなたの質問にお答えする間に、どれほど難しいかを隠すかもしれません! '<>'の代わりに '{{' 'を使うと、"> _ <<3 I luv you => _o/"のようなコメントを解析することで正規表現を悪夢に変えることができます。 あなたの頭の正規表現ではシンプルな "これを探す"というのはありません! htmlの正規表現を解析するには、毎回再帰的に行って、最初に戻っていかなければなりません。パーサーを使用すると、jsでコードを実行するのと同じようにコードが簡単になります。 –

+0

ありがとう、私はあなたの意見を大事にしていますが、RegExは簡単なアプローチのようですが、そうではないようです。 WordPressのdoShortCode()が達成したことと同様のことをやろうとしているときに 'SubString'と' IndexOf'に移動しようとしましたが、現在どのように動作しているのかのドキュメントを見つけることができました。私はコンセプトの証明を取得し、そこから移動するために探しています。 – Derek

+0

Htmlパーサーを[Html Agility Pack(HAP)](http://html-agility-pack.net/?z=codeplex)として使用します。シンプルなナゲットとビムで、あなたはhtmlで何でも好きなものを選ぶことができます。何も学ぶことのないところにいることを学ぶのは難しいことではありません。 –

答えて

0

を私は正規表現と私の最初の質問を解決していないですが、私は簡単な解決策に移動しました:

は、この私が潜在的に使用できる例です。 SubStringIndexOfstring.Splitを当面使用すると、私のコードを整理する必要があると私は理解していますが、これまでの回答を投稿すると思いました。

string html = "<p>Start of Example</p>{{Object id='foo'}}<p>End of example</p>" 
string strObject = "Slider"; //Example 

//When found, this will contain "{{Object id='foo'}}" 
string strCode = ""; 

//ie: "id='foo'" 
string strCodeInner = ""; 

//Tags will be a list, but in this example, only "id='foo'" 
string[] tags = { }; 

//Looking for the following "{{Object " 
string strFindStart = "{{" + strObject + " "; 
int intFindStart = html.IndexOf(strFindStart); 

//Then ending in the following 
string strFindEnd = "}}"; 
int intFindEnd = html.IndexOf(strFindEnd) + strFindEnd.Length; 

//Must find both Start and End conditions 
if (intFindStart != -1 && intFindEnd != -1) 
{ 
    strCode = html.Substring(intFindStart, intFindEnd - intFindStart); 

    //Remove Start and End 
    strCodeInner = strCode.Replace(strFindStart, "").Replace(strFindEnd, ""); 

    //Split by spaces, this needs to be improved if more than IDs are to be used 
    //but for proof of concept this is perfect 
    tags = strCodeInner.Split(new char[] { ' ' }); 
} 

Dictionary<string, string> dictTags = new Dictionary<string, string>(); 
foreach (string tag in tags) 
{ 
    string[] tagSplit = tag.Split(new char[] { '=' }); 
    dictTags.Add(tagSplit[0], tagSplit[1].Replace("'", "").Replace("\"", "")); 
} 

//At this point, I can replace "{{Object id='foo'}}" with anything I'd like 
//What I don't show is that I go into the website's database, 
//get the object (ie: Slider) and return the html for slider with the ID of foo 
html = html.Replace(strCode, strView); 

/* 
    "html" variable may contain: 

    <p>Start of Example</p> 
    <p id="foo">This is the replacement text</p> 
    <p>End of example</p> 

*/