regex
  • vb.net
  • 2010-12-19 3 views 0 likes 
    0

    을 필요로 교체 :정규식 도움의이 나는 HTML 문자열 있다고 가정 해 봅시다

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> 
    <head> 
    </head> 
    <body> 
    <p>GRANDMÈRE Break the fillets of the saucepan on a double and shaped into neat pieces and stir it boil hard, or of nutmeg and salt. Throw them fry as a few inches by one in this very well. Put the whites of butter by three. Put some artichoke-bottoms cooked green</p> 
    <p>darkly colored on half with a little flour MY_IDENTIFIER and midrib. Put a hot for each side of vanilla cream as you cannot give it, and dish is a cauliflower, which you have not very useful sauce from the inside with a little nutmeg, and serve with the King of water</p> 
    <p>dining-room. At the meat. STUFFED CAULIFLOWER SOUP (BELGIAN RECIPE) Take three quarters of tying the juice of ham. Keep the pot, so as being interpreted, means that time put them into four bay salt, and chopped. When the better to sprinkle in the tomato much as many crescents one of</p> 
    <p>touch the rabbit to put quickly in. A white wine glass cups and pour over them, cut them in salt, pepper, and fill them in a pint of the liquor; it is a poached on slowly, without a layer of an egg on the yolks, and mix very clean, while</p> 
    <p>CAKE, EXCELLENT FOR PASTRY Equal quantities of red wine. Stew your taste, use that, with extract and salt and ham, mushrooms when the mold and dip them a good red wine. This dish with pepper and place meat and serve with a good foundation for twenty potatoes, and potato, some</p> 
    <p>half-an-hour. GOLDEN RICE Put them very little MY_IDENTIFIER book on a glass dish that way. CABBAGE WITH CHEESE Every one and season it up with not enough to make a pat of butter, each round quickly. Or add, instead of fresh lean and let it every now and put it melts</p> 
    <p>leek, and over it, a half a fireproof cases from burning. CHOU-CROUTE Take the salad you take out the amount of cream is not get in four, about three-and-a-half pints of the middle of this sauce some chopped almonds, chopped parsley and mix it in your pieces of grated cheese</p> 
    <p>sides. In four or flageolets, and stir in company with flour, and let it out, and pour over all, chop your vinegar to half a lemon--this would do not quite, add the edges. Steep them in a tablespoonful of butter and mustard. Take it in salted water; and, crumbling out</p> 
    <p>care that it in which you have seasoned with an equal size, mix MY_IDENTIFIER these are well with the fermentation has a custard. Put the top with a very carefully, so that you have added at a sieve; or, for at home than thick. Then fry the custard as you prepare</p> 
    <p>stuffing into a fireproof dish, and fry them to picnics, or marjoram with this MY_IDENTIFIER way besides parsley. Roll them out neatly with vanilla, a tablespoonful of mustard, pepper and salt, then pour it all cooked, and it to be ready to keep it simmer it over and salt. The original</p> 
    </body> 
    </html>

    을 나는 P 태그를 찾을 필요와 텍스트가 포함 된 경우 "MY_IDENTIFIER는"그 텍스트에 몇 가지 조작을하고 교체 텍스트 일부 텍스트입니다.

    여기 정규식을 사용하여 텍스트로 단락 태그를 찾는 방법을 알고 있습니다. 경기를 반복 할 수 있으며 필요에 따라 텍스트로 조작 할 수 있습니다. 일치하는 항목을 다른 텍스트로 바꾸는 방법을 알고 싶습니다.

    위의 예에서 2, 6, 9 및 10 단락에 "MY_IDENTIFIER"이 있습니다. ...의 난 등

    <p>6th paragraph text</p>

    과 같은

    <p>2nd paragraph text</p>

    , 6 단락 텍스트로 두 번째 단락 텍스트를 교체하고 싶은 말을

    내가 지금까지 가지고있는 코드를 보자 ...

    Imports System.Text.RegularExpressions 
    
    Module modMain 
    
        Sub main() 
         Dim fileContents As String 
         fileContents = My.Computer.FileSystem.ReadAllText("C:\temp\a.html") 
         Dim paras As MatchCollection = Regex.Matches(fileContents, "<p>(.+?MY_IDENTIFIER.+?)</p>") 
         Dim TxtFound As String 
         For Each oMatch As Match In paras 
          TxtFound = oMatch.Groups(1).Value 
          'do some manipulations with txtfound 
          '... 
          'replace the txtfound with some other text 
    
         Next 
    
         'Save the file again 
        End Sub 
    End module

    도움을 주시면 감사하겠습니다.

    답변

    0

    내가 먼저 글로벌 경기를 통해 모든 단락 찾기 위해 시도 할 것이다 :

    my @matches = ($string =~ m!<p>(.*?)</p>!sig); 
    

    그럼 난을 통해 루프 것, 그리고 당신의 식별자를 포함하는 그 어떤 대체 :

    foreach(@matches) { 
        #keep a copy for substitution below 
        my $before = $_; 
    
        #if the identifier is found, replace it 
        if($_ =~ s!MY_IDENTIFIER!replacement text!is) { 
        #then take the newly replaced text, and replace it in your original $string variable 
        $string =~ s!$before!$_!is; 
        } 
    } 
    
    관련 문제