2016-08-21 6 views
-1

ファイル内の式<disp-formula id="deqn(\d+)-(\d+)">を探して、1つ以上の一致がある場合は、ファイル全体を検索して<xref ref-type="disp-formula" rid="deqnX">(X)</xref>または<xref ref-type="disp-formula" rid="deqnX">X</xref>ここで最後(\ D +)に第(\ D +)からX範囲及び<xref ref-type="disp-formula" rid="deqn$1-$2">(X)</xref>又は<xref ref-type="disp-formula" rid="deqn$1-$2">X</xref>ファイル内のデータを解析した後にリンクタグを変更する

サンプル入力にそれを置き換える:

<sec id="sec1"> 
<para>In addition, many of you will be glad to hear that <xref ref-type="disp-formula" rid="deqn1">(1)</xref> Visual Basic is now a fully object-oriented programming language <xref ref-type="disp-formula" rid="deqn3">(3)</xref>-<xref ref-type="disp-formula" rid="deqn5">(5)</xref>, with the inclusion of the long sought-after class inheritance, as well as other OOP features.</para> 
</sec> 
<para>In this chapter, you'll see how Visual Basic has evolved eq. <xref ref-type="disp-formula" rid="deqn1">1</xref> into the VB .NET language of today and get some sense of how and why VB .NET is different from previous versions of Visual Basic.</para> 
<sec id="sec1a"> 
<para>How had I ever managed living without him? 
<disp-formula id="deqn1-2">$$\phi=a+b-c^2$$</disp-formula></para> 
<para>Gideon Cross. <xref ref-type="figure" rid="fig2">Figure 2</xref>, table 3.</para> 
<para>This chapter surveys some of the new features of the .NET Framework <xref ref-type="disp-formula" rid="deqn2">(2)</xref>, <xref ref-type="disp-formula" rid="deqn5">(5)</xref> that most impact the VB developer. These include namespaces, the Common Language Runtime (CLR), and assemblies.</para> 
<para>The third and final section, Part III, consists of the following appendixes: 
<disp-formula id="deqn3-5">$$m=a+b-c$$</disp-formula> 
</para> 
</sec> 
</sec> 
<sec id="sec2"> 
<label>2.</label> 
<disp-formula id="deqn6">$$\cal {X}=y=1$$</disp-formula> 
<para>The switch…case Statement.</para> 
<para>A discussion of language changes <xref ref-type="disp-formula" rid="deqn6">(6)</xref> from VB 6 to VB .NET.</para> 
<para>A list of VB .NET intrinsic constants, as well as VB .NET enumerations and their members.</para> 
</sec> 

所望の出力:

<sec id="sec1"> 
<para>In addition, many of you will be glad to hear that <xref ref-type="disp-formula" rid="deqn1-2">(1)</xref> Visual Basic is now a fully object-oriented programming language <xref ref-type="disp-formula" rid="deqn3-5">(3)</xref> and <xref ref-type="disp-formula" rid="deqn3-5">(5)</xref>, with the inclusion of the long sought-after class inheritance, as well as other OOP features.</para> 
</sec> 
<para>In this chapter, you'll see how Visual Basic has evolved eq. <xref ref-type="disp-formula" rid="deqn1-2">1</xref> into the VB .NET language of today and get some sense of how and why VB .NET is different from previous versions of Visual Basic.</para> 
<sec id="sec1a"> 
<para>How had I ever managed living without him? 
<disp-formula id="deqn1-2">$$\phi=a+b-c^2$$</disp-formula></para> 
<para>Gideon Cross. <xref ref-type="figure" rid="fig2">Figure 2</xref>, table 3.</para> 
<para>This chapter surveys some of the new features of the .NET Framework <xref ref-type="disp-formula" rid="deqn1-2">(2)</xref>, <xref ref-type="disp-formula" rid="deqn3-5">(5)</xref> that most impact the VB developer. These include namespaces, the Common Language Runtime (CLR), and assemblies.</para> 
<para>The third and final section, Part III, consists of the following appendixes: 
<disp-formula id="deqn3-5">$$m=a+b-c$$</disp-formula> 
</para> 
</sec> 
</sec> 
<sec id="sec2"> 
<label>2.</label> 
<disp-formula id="deqn6">$$\cal {X}=y=1$$</disp-formula> 
<para>The switch…case Statement.</para> 
<para>A discussion of language changes <xref ref-type="disp-formula" rid="deqn6">(6)</xref> from VB 6 to VB .NET.</para> 
<para>A list of VB .NET intrinsic constants, as well as VB .NET enumerations and their members.</para> 
</sec> 

これは私は誰もがこの上で私を助けることができる、これまでに

Dim targetDirectory As String = TextBox1.Text 
     Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory, "*.txt") 
     For Each txtFile In txtFilesArray 
      Dim input As String = File.ReadAllText(txtFile) 
      Dim disp As New Regex("<disp-formula id=""deqn(\d+)-(\d+)"">") 
      Dim match As Match = disp.Match(input) 
      If disp.Matches() Then 
       Dim a As Integer = match.Groups(1).Value 
       Dim b As Integer = match.Groups(2).Value 
       For Each c=a to b in input 
         Dim xref As New Regex("<xref[^>]+rid=""(?<id>deqn\d+-\d+)""[^>]*>(?<content>[^<]+)</xref>") 
        Dim result As String = xref.Replace(input, Function(xyz) 
                    ??????? 
                   End Function) 
       Next 

       File.WriteAllText(txtFile, result) 
      End If 
     Next 

を試みた何ですか?

+0

ただのコメントで解決策ではありません。質問を絞り込むことはできますか?さらに、状況に応じてパーサーが適切かもしれません。 – Jan

+0

私は2,3週間前に同様の質問に答えました。 xmlパーサーを使用する方がはるかに簡単です。 http://stackoverflow.com/questions/38938984/how-to-use-matchevaluator-function-to-reduce-coding/38940068#38940068 – FloatingKiwi

+0

@FloatingKiwi XML解析技術に精通していないxml解析技術を使用せずにそれを達成する。コードが少し大きくなるのであれば、私は気にしません。 –

答えて

0

ここでは、私があなたや他の誰かのために書いたコメント付きのUltraEditスクリプト(JavaScript)をほぼ同じタスクのために紹介します。

詳細についてはUltraEdit - ScriptsフォーラムのトピックScript to modify linking tagsを参照してください。

いくつかの小さな違いがあります。

  1. タグ<formulaは、UEのフォーラムでは、今ここに<disp-formulaです。
  2. UEフォーラムのid/rid属性値dqnはここに今すぐdeqnです。 UEフォーラムにおける属性値d-formuladisp-formulaです。
  3. xrefの参照番号は、常にUEフォーラムのカッコで囲まれていましたが、ここでは括弧で囲んでもかまいません。

私がUltraEditフォーラムに投稿したスクリプトコードは、4つの小さな違いによって私に適応されました。これにより、再フォーマットされたファイルの各参照値は常に括弧で囲まれています。

function outputDebugInfo(sDebugInfo) 
{ 
// UltraEdit.outputWindow.write(sDebugInfo); 
} 

if (UltraEdit.document.length > 0) // Is any file opened? 
{ 
    // Define environment for this script. 
    UltraEdit.insertMode(); 
    UltraEdit.columnModeOff(); 

    // Define from which file to read the formula identifiers. 
    var oFormulaDoc = UltraEdit.activeDocument; 

    var nRefIndex; 
    var nFormulaIndex; 
    var anFormulas = new Array(); 

    // Select the entire file with the formula identifiers. 
    // There is nothing selected if the file is an empty file. 
    oFormulaDoc.selectAll(); 
    if (oFormulaDoc.isSel()) 
    { 
     // Get all formula identifiers with a single number and convert each 
     // number from string to integer. The formula identifier numbers are 
     // stored in a two dimensional array. On a single number first and 
     // second number are identical in the two columns in the current row. 
     var asFormulas = oFormulaDoc.selection.match(/<disp-formula id="deqn\d+"/g); 
     if (asFormulas != null) 
     { 
      for (nFormulaIndex = 0; nFormulaIndex < asFormulas.length; nFormulaIndex++) 
      { 
       var nFormula = parseInt(asFormulas[nFormulaIndex].replace(/^.+deqn(\d+).$/,"$1"),10); 
       anFormulas.push([nFormula,nFormula]); 
      } 
     } 

     // Get all formula identifiers with a number range and convert 
     // the two numbers of each number range from string to integer. 
     // The FROM number is stored in first column of formula row and 
     // the TO number is stored in second column. 
     var asFormulas = oFormulaDoc.selection.match(/<disp-formula id="deqn\d+-\d+"/g); 
     if (asFormulas != null) 
     { 
      for (nFormulaIndex = 0; nFormulaIndex < asFormulas.length; nFormulaIndex++) 
      { 
       var nFrom = parseInt(asFormulas[nFormulaIndex].replace(/^.+deqn(\d+)-.+$/,"$1"),10); 
       var nTo = parseInt(asFormulas[nFormulaIndex].replace(/^.+deqn\d+-(\d+).$/,"$1"),10); 
       anFormulas.push([nFrom,nTo]); 
      } 
     } 

     // Cancel the selection and move caret to top of file. 
     oFormulaDoc.top(); 
    } 

    // The formula references can be only checked and updated 
    // if there was before at least 1 formula identifier found. 
    if (anFormulas.length > 0) 
    { 
     // Dumps formular identifiers table to output window. 
     // var_dump(anFormulas); 

     var nRefsFound = 0;  // Counts the number of found formula reference sequences. 
     var nRefsModified = 0; // Counts the number of modified formula reference sequences. 

     // Define start tag with the attributes and end tag of a formula reference. 
     var sRefStart = '<xref ref-type="disp-formula" rid="deqn'; 
     var sRefEnd = '</xref>'; 

     // Define some search and replace strings used in the main loop below. 
     var sFindRefs = sRefStart + '[\\d\\-]+">.+?' + sRefEnd + '(?:[\\s,-]*(?:&#x2013;\\s*)?' + 
          sRefStart + '[\\d\\-]+">.+?' + sRefEnd + ')*'; 
     outputDebugInfo("sFindRefs = " + sFindRefs); 

     var sReplRange = ')' + sRefEnd + '#' + sRefStart + '$1">($1'; 
     outputDebugInfo("sReplRange = " + sReplRange); 

     var sRemoveRef = sRefStart + '\\d+">\\(?(\\d+)\\)?' + sRefEnd; 
     outputDebugInfo("sRemoveRef = " + sRemoveRef); 
     var oRemoveRef = new RegExp(sRemoveRef,"g"); 

     // A Perl regular expression find is used to find 1 or more formula 
     // references in a loop which are processed and if needed updated. The 
     // formula references are searched and processed always on active file. 
     UltraEdit.perlReOn(); 
     UltraEdit.activeDocument.findReplace.mode=0; 
     UltraEdit.activeDocument.findReplace.matchCase=true; 
     UltraEdit.activeDocument.findReplace.matchWord=false; 
     UltraEdit.activeDocument.findReplace.regExp=true; 
     UltraEdit.activeDocument.findReplace.searchDown=true; 
     UltraEdit.activeDocument.findReplace.searchInColumn=false; 

     UltraEdit.activeDocument.top(); 
     while (UltraEdit.activeDocument.findReplace.find(sFindRefs)) 
     { 
      nRefsFound++; 
      var sReferencesFound = UltraEdit.activeDocument.selection; 
      outputDebugInfo("\nsReferencesFound = " + sReferencesFound); 

      // Replace - and &#x2013; with or without surrounding whitespaces 
      // between > and < (tags) or between) and ((reference numbers) 
      // by a hash. Hash character is used as it does not exist anymore 
      // in the reference sequence string after this replace in contrast 
      // to dash character and # is no regular expression character. 
      var sRefModified = sReferencesFound.replace(/([)>])\s*(?:-|&#x2013;)\s*([(<])/g,"$1#$2"); 

      // Remove all whitespaces and commas between the tags. 
      sRefModified = sRefModified.replace(/>[\s,]+</g,"><"); 

      // Replace already existing rid attribute values with a range 
      // by rid attribute value with a single reference number. 
      sRefModified = sRefModified.replace(/\d+-\d+\">\(?(\d+)\)?/g,'$1">($1)'); 

      // Convert a reference number range like (5)#(10) into a complete 
      // reference range like <xref ref-type="disp-formula" rid="deqn5"> 
      // (5)</xref>#<xref ref-type="disp-formula" rid="deqn10">(10)</xref> 
      sRefModified = sRefModified.replace(/\)#\(?(\d+)/g,sReplRange); 
      outputDebugInfo("sRefModified (1) = " + sRefModified); 

      // Remove the reference start and end tags to get just the reference 
      // single numbers and reference number ranges separated by comma. 
      sRefModified = sRefModified.replace(oRemoveRef,"$1,"); 

      // Insert a comma after each hash character used to mark a range. 
      sRefModified = sRefModified.replace(/#/g,"#,"); 

      // Remove the comma at end of the string. 
      sRefModified = sRefModified.substr(0,sRefModified.length-1); 
      outputDebugInfo("sRefModified (2) = " + sRefModified); 

      // Split up the reference numbers and reference ranges 
      // for cross-checking them with the formula identifiers. 
      var asReferences = sRefModified.split(","); 

      // Convert the number strings into integer numbers and replace 
      // also the ranges by appropriate sequences of integer numbers. 
      var nRefNumber; 
      var anRefNumbers = new Array(); 
      for (nRefIndex = 0; nRefIndex < asReferences.length; nRefIndex++) 
      { 
       if (asReferences[nRefIndex] != '#') 
       { 
        nRefNumber = parseInt(asReferences[nRefIndex],10); 
        anRefNumbers.push(nRefNumber); 
       } 
       else 
       { 
        var nEndNumber = parseInt(asReferences[++nRefIndex],10); 
        while (nRefNumber < nEndNumber) anRefNumbers.push(++nRefNumber); 
       } 
      } 
      outputDebugInfo("anRefNumbers  = " + anRefNumbers.join(",")); 

      // Append to the array the value 0 to end any sequence always 
      // with in the next loop processing the formula references. 
      anRefNumbers.push(0); 

      /* Rebuilt the reference(s) by putting together as much formula 
       references as possible. 

       The base rule for putting a sequence of references together 
       is: The reference numbers are one after the other with each 
       reference number being +1 of previous reference number and 
       no gap between. 

       There are three types of ranges: 

      1. A reference number range within a single reference because the 
       formula identifiers are already put together. E.g. there is 

        <disp-formula id="deqn2-6">...</formula> 

       and the reference is 

        <xref ref-type="disp-formula" rid="deqn2-6">(3)&#x2013;(5)</xref> 

       It is possible for this type of a range that end number is 
       just start number plus 1, for example 

       <xref ref-type="disp-formula" rid="deqn2-6">(5)&#x2013;(6)</xref> 

      2. A formula reference range because the formula identifiers 
       are still separated. For example there are 

        <disp-formula id="deqn7">...</formula> 
        <disp-formula id="deqn8">...</formula> 
        <disp-formula id="deqn9">...</formula> 

       and the matching reference range is 

        <xref ref-type="disp-formula" rid="deqn7">(7)</xref> 
        <xref ref-type="disp-formula" rid="deqn8"/>&#x2013; 
        <xref ref-type="disp-formula" rid="deqn9">(9)</xref> 

      3. The third range type is a combination of first and second like 

        <xref ref-type="disp-formula" rid="deqn2-6">(3)</xref> 
        <xref ref-type="disp-formula" rid="deqn7"/> 
        <xref ref-type="disp-formula" rid="deqn8"/>&#x2013; 
        <xref ref-type="disp-formula" rid="deqn9">(9)</xref> 

       But not put together with a dash is a sequence of exactly 
       2 references with different formula identifiers for first 
       and second reference like 

        <disp-formula id="deqn7">...</formula> 
        <disp-formula id="deqn8">...</formula> 

       referenced with 

        <xref ref-type="disp-formula" rid="deqn7">(7)</xref>, 
        <xref ref-type="disp-formula" rid="deqn8">(8)</xref> 
      */ 

      var nFormulaStart = -1; 
      var nFormulaEnd  = -1; 
      var nReferenceStart = -1; 
      var nReferenceEnd = -1; 
      var nPreviousNumber = -1; 

      nRefIndex = 0; 
      sRefModified = ""; 

      while(true) // This loop is exited when a reference number 
      {    // with the value 0 is loaded from the array. 
       var bFormulaFound = false; 
       nRefNumber = anRefNumbers[nRefIndex]; 

       if(nRefNumber > 0) 
       { 
        // Find the matching formula identifier for this reference. 
        nFormulaIndex = 0; 
        do 
        { 
         if ((nRefNumber >= anFormulas[nFormulaIndex][0]) && 
          (nRefNumber <= anFormulas[nFormulaIndex][1])) 
         { 
          bFormulaFound = true; 
          break; 
         } 
        } 
        while(++nFormulaIndex < anFormulas.length); 
       } 

       // Is there no formula matching the reference number or is the 
       // current reference number not +1 of previous reference number 
       // and the reference is not the first one of a possible range? 
       if((!bFormulaFound || (nRefNumber != (nPreviousNumber+1))) && (nPreviousNumber > 0)) 
       { 
        // Append single or start formula reference. 
        if (nFormulaStart >= 0) 
        { 
         // Append a comma and a space if the rebuilt 
         // reference string is not empty anymore. 
         if (sRefModified.length != 0) sRefModified += ", "; 

         sRefModified += sRefStart + anFormulas[nFormulaStart][0]; 
         if (anFormulas[nFormulaStart][0] != anFormulas[nFormulaStart][1]) 
         { 
          sRefModified += "-" + anFormulas[nFormulaStart][1]; 
         } 
         sRefModified += '">(' + nReferenceStart + ')' 

         // Is there a reference range processing in progress? 
         if(nFormulaEnd >= 0) 
         { 
          // Is this a reference range of first type? 
          if (nFormulaStart == nFormulaEnd) 
          { 
           sRefModified += '&#x2013;(' + nReferenceEnd + ')'; 
          } 

          // Are there only 2 references and they don't reference the 
          // same formula identifier, interpret them separated with 
          // a comma and not as formula reference range with a dash. 
          else if ((nReferenceStart+1) == nReferenceEnd) 
          { 
           sRefModified += sRefEnd + ", " + sRefStart + anFormulas[nFormulaEnd][0]; 
           if (anFormulas[nFormulaEnd][0] != anFormulas[nFormulaEnd][1]) 
           { 
            sRefModified += "-" + anFormulas[nFormulaEnd][1]; 
           } 
           sRefModified += '">(' + nReferenceEnd + ')'; 
          } 

          else // More than 2 references with different 
          {  // start and end formula identifiers. 
           sRefModified += sRefEnd; 

           // Append the empty formula references between start and 
           // end reference number depending on existing formulas. 
           var nSequenceRef = anFormulas[nFormulaStart][1]; 
           while (++nSequenceRef < anFormulas[nFormulaEnd][0]) 
           { 
            for(var nSequenceIndex = 0; nSequenceIndex < anFormulas.length; nSequenceIndex++) 
            { 
             if ((nSequenceRef >= anFormulas[nSequenceIndex][0]) && 
              (nSequenceRef <= anFormulas[nSequenceIndex][1])) 
             { 
              sRefModified += sRefStart + anFormulas[nSequenceIndex][0]; 
              if (anFormulas[nSequenceIndex][0] != anFormulas[nSequenceIndex][1]) 
              { 
               nSequenceRef = anFormulas[nSequenceIndex][1]; 
               sRefModified += "-" + nSequenceRef; 
              } 
              sRefModified += '"/>'; 
              break; 
             } 
            } 
           } 

           // Append end formula reference. 
           sRefModified += '&#x2013;' + sRefStart + anFormulas[nFormulaEnd][0]; 
           if (anFormulas[nFormulaEnd][0] != anFormulas[nFormulaEnd][1]) 
           { 
            sRefModified += "-" + anFormulas[nFormulaEnd][1]; 
           } 
           sRefModified += '">(' + nReferenceEnd + ')'; 
          } 
          nFormulaEnd = -1; 
          nReferenceEnd = -1; 
         } 
         nFormulaStart = -1; 
         nReferenceStart = -1; 
         sRefModified += sRefEnd; 
        } 
       } 

       // Are all reference numbers in array processed? 
       if (nRefNumber < 1) break; 

       // Is there no formula for the current formula reference number? 
       if (!bFormulaFound) 
       { 
        // This case should never occur, but must be nevertheless 
        // taken into account to avoid corruption of file content. 

        // Append a comma and a space if the rebuilt 
        // reference string is not empty anymore. 
        if (sRefModified.length != 0) sRefModified += ", "; 

        // Append a formula reference like when the formula would really exist. 
        sRefModified += sRefStart + nRefNumber + '">(' + nRefNumber + ')' + sRefEnd; 
        nPreviousNumber = -1; 

        // Output a warning message to output window and make the output 
        // window visible if not already visible on running the script. 
        if (UltraEdit.outputWindow.visible == false) 
        { 
         UltraEdit.outputWindow.showWindow(true); 
        } 
        UltraEdit.outputWindow.write("WARNING: Found no formula for formula reference " + nRefNumber); 
       } 
       else 
       { 
        // Is the current reference number +1 of previous number? 
        if (++nPreviousNumber == nRefNumber) 
        { 
         nReferenceEnd = nRefNumber; 
         nFormulaEnd = nFormulaIndex; 
        } 
        else // First reference of a possible new range. 
        { 
         nPreviousNumber = nRefNumber; 
         nReferenceStart = nRefNumber; 
         nFormulaStart = nFormulaIndex; 
        } 
       } 
       nRefIndex++; // Next reference from array of references. 
      } 

      if (sRefModified != sReferencesFound) 
      { 
       nRefsModified++; 
       // Overwrite the found reference sequence by rebuilt string. 
       UltraEdit.activeDocument.write(sRefModified); 
       outputDebugInfo("sRefModified (3) = " + sRefModified); 
      } 
     } 

     UltraEdit.activeDocument.top(); 
     UltraEdit.messageBox("Number of reference sequences found/modified: " + nRefsFound + "/" + nRefsModified); 
    } 
} 

スタックオーバーフロー上の誰かが無料であなたの会社のためのVB.Netコードなどの一部のウルトラ拡張子を持つこのJavaScriptコードを書き換えた場合を見てみましょう。

+0

最初に、私はUltraEditフォーラムアカウントを持っていないので、他の誰かと私を間違えている(あなたがフォーラムで説明したタスクは私のものとほぼ同じですが)最後に、私はタスクを完了するためのプロセスを持っていましたが、完了するのに苦労していたので、私が試したコードを投稿し、誰かが私がコードに間違っていることを指摘し、私はプログラム全体、だからおいおい... –

関連する問題