0
public static String convert(String str)
{
  if (str.equals("# "))
     System.out.println(" ");
  Pattern pattern = Pattern.compile("(#+[^#]+)");
  Matcher matcher = pattern.matcher(str);

  while (matcher.find())
  {
     String str1 = matcher.group(1);
     if (str1.replaceFirst("#+", "").length() == 0 || str1.replaceFirst("#+", "").matches("[\\s]+"))
        continue;
     int n = str1.length() - str1.replaceFirst("#+", "").length();
     System.out.println("<h" + n + ">" + str1.substring(n) + "</h" + n + ">");
  }
  char carac;
  carac = str.charAt(0);
  if (carac >= 65 && carac <= 90)
  {
     System.out.println("<p>");
     System.out.println(str);
     System.out.println("</p>");
  }
  return ("");
}

while(matcher.findをこれと組み合わせると、次のif(carac>=出力が得られます: < p> < h2> Decibel < /h2> < /p> for this input: ##Decibel??? (代わりに < h2> Decibel < /h2> を取得します。文頭の「#」を認識して < h1> < /h1> または < hn> に変換するアルゴリズムを作成しています。その後、アルゴリズムは文頭に大文字があるかどうかを認識し、ある場合は段落の最初と最後に < p> < /p> を追加します. 大きな問題私が持っているのは、両方を結合できるようにしたいということです。つまり、#Appendix があるとしましょう。それを < p> < h1> Appendix < /h1> < /p> に変換したいと思います。

ありがとうございました

4

1 に答える 1

0
   public static String convert(String str)
 {
   if (str.equals("# "))
      System.out.println(" ");
   Pattern pattern = Pattern.compile("(#+[^#]+)");
   Matcher matcher = pattern.matcher(str);

   while (matcher.find())
   {
       boolean append_p = false;
       char carac;
        for(int i = 0; i < str.length(); i++){
       if(Character.isLetter(str.charAt((i)))){
           if (Character.isUpperCase(str.charAt(i)))
           {
              System.out.print("<p>");
              append_p = true;
              break;
           }
       }else{
           append_p = false;
       }
   }
      String str1 = matcher.group(1);
      if (str1.replaceFirst("#+", "").length() == 0 || str1.replaceFirst("#+", "").matches("[\\s]+"))
         continue;
      int n = str1.length() - str1.replaceFirst("#+", "").length();
      System.out.print("<h" + n + ">" + str1.substring(n) + "</h" + n + ">");
      if(append_p == true){ System.out.print("</p>"); append_p = false;}
   }
   if(Character.isUpperCase(str.charAt(0)){
      System.out.println("<p>"+str+"</p>");
   return ("");
 }

これは、最初の文字が大文字で、append/prepend かどうかをテストします<p></p>

于 2012-10-25T06:18:38.893 に答える