[英]Markdown algorithm: string difficulties
I started writing this algorithm: 我开始编写此算法:
public static String convert(String str) {
if (str.equals("# "))
return " ";
if (str.matches("#+.+")) {
int n = str.length() - str.replaceFirst("#+", "").length();
return "<h" + n + ">" + str.substring(n) + "<h" + n + ">";
}
return str;
}
}
So when I type, ####title, it returns < h4>title< /h4> 因此,当我键入#### title时,它将返回<h4> title </ h4>
My problem is that when I write ####title###title, I would like it to return < h4>title< /h4> < h3>title< /h3> but it only returns < h4>title< /h4>...What am I doing wrong??? 我的问题是,当我写#### title ### title时,我希望它返回<h4> title </ h4> <h3> title </ h3>,但它只返回<h4> title </ h4 > ...我在做什么错???
Thats because you are using the pattern: - #+.+
. 那是因为您使用的是模式:-
#+.+
。
Now, since .
现在,因为
.
matches everything in Regex, so in the above pattern, it matches everything
after an initial set
of #'s
. 匹配正则表达式中的所有内容,因此在上述模式中,它匹配
initial set
#'s
之后的everything
。
So, for your input: - ####title###title , your pattern will match: - 因此,对于您的输入:- #### title ### title ,您的模式将匹配:-
#+
will match ####
#+
将匹配####
.+
will match title###title
.+
将匹配title###title
You need to change your regex to : - (#+[^#]+)
, and probably need to use Pattern class here to get the desired output, becaues you want to match every
part of your string to the given pattern
. 您需要将正则表达式更改为:-
(#+[^#]+)
,并且可能需要在此处使用Pattern类来获取所需的输出,因为您希望将字符串的every
部分都与给定的pattern
匹配。
#+[^#]+
-> Will match the first set of #
and then everything after that, except #
. #+[^#]+
->将匹配第一组#
,然后匹配除#
之外的所有内容。 So it stops where the next set of #'s
start. 因此它停止了下一组
#'s
开始。
Here's how you can use it: - 使用方法如下:-
String str = "####title###title"; // str is the method parameter
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 + ">");
}
OUTPUT : - 输出 :-
<h4>title</h4>
<h3>title</h3>
You are replacing only the first occurrence of #+. 您只替换第一次出现的#+。 Try replacing the if with a while and instead of return inside the if, append the result into a StringBuilder.
尝试将if替换为一会儿,而不是在if内部返回,而是将结果附加到StringBuilder中。
Something like: 就像是:
String str = "####title###title2";
StringBuilder sb = new StringBuilder();
while (str.matches("#+.+")) {
int n = str.length() - str.replaceFirst("#+", "").length();
str = str.replaceFirst("#+", "");
int y = str.length();
if(str.matches(".+#+.+")) {
y = str.indexOf("#");
sb.append( "<h" + n + ">" + str.substring(0,y) + "<h" + n + ">");
str = str.substring(y, str.length());
} else {
sb.append( "<h" + n + ">" + str.substring(0,y) + "<h" + n + ">");
}
}
System.out.println(sb.toString());
}
You are matching wrong string, try this one: 您匹配了错误的字符串,请尝试以下方法:
#+[^#]+
And of course you want to make call it recursivly or in a loop 当然,您要递归或循环调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.