简体   繁体   English

降价算法:#号后接Enter的序列应不返回任何内容

[英]Markdown algorithm: sequence of # followed by enter should return nothing

So now I have this method: 所以现在我有了这个方法:

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);
    int n = str1.length() - str1.replaceFirst("#+", "").length();
System.out.println("<h" + n + ">" + str1.substring(n) + "</h" + n + ">");
}

return ("");
}    

When I type ###Le Monde # it gives me < h3>Le Monde < /h3> < h1> < /h1>. 当我输入### Le Monde#时,它会给我<h3> Le Monde </ h3> <h1> </ h1>。 I would like it to ignore the # following "le monde". 我希望忽略“ le monde”之后的#号。 Basically, I want the algorithm to ignore a series of # followed by a space or return key. 基本上,我希望算法忽略一系列#后跟一个空格或返回键。 What am I doing wrong? 我究竟做错了什么?

Add another line after getting the next match (str1), so: 在获得下一个匹配项(str1)之后添加另一行,因此:

String str1 = matcher.group(1);
if(str1.replaceFirst("#+", "").length() == 0 || str1.replaceFirst("#+", "").matches("[\\s]+")) continue;

That will ignore any whitespaces matches. 这将忽略任何空格匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM