我有一个正则表达式,它使用一种"'''.*?'''|'.*?'"
模式来查找三引号 (''') 和单引号 (') 之间的文本。当回车添加到输入字符串时,正则表达式模式无法读取到三引号的末尾。知道如何将正则表达式更改为读取到三次刻度的末尾而不是在 \n 上中断吗?quoteMatcher.end() 返回值 2 所以下面的失败情况返回''''''
作品:
'''<html><head></head></html>'''
失败:
用户输入值:
'''<html>
<head></head>
</html>'''
Java 表示:
'''<html>\n<head></head>\n</html>'''
解析逻辑:
public static final Pattern QUOTE_PATTERN = Pattern.compile("'''.*?'''|'.*?'");
Matcher quoteMatcher = QUOTE_PATTERN.matcher(value);
int normalPos = 0, length = value.length();
while (normalPos < length && quoteMatcher.find()) {
int quotePos = quoteMatcher.start(), quoteEnd = quoteMatcher.end();
if (normalPos < quotePos) {
copyBuilder.append(stripHTML(value.substring(normalPos, quotePos)));
}
//quoteEnd fails to read to the end due to \n
copyBuilder.append(value.substring(quotePos, quoteEnd));
normalPos = quoteEnd;
}
if (normalPos < length) copyBuilder.append(stripHTML(value.substring(normalPos)));