我希望匹配所有内容,包括&#34; {&#34;,&#34; {{{&#34;,以及达到&#34时的匹配停止; {{&#34;。< / p>
我尝试使用以下代码,但当它到达&#34; {{{&#34;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="Mytable">
<thead>
<tr>
<td>Id</td>
</tr>
<tr>
<td>Fullname</td>
</tr>
<tr>
<td>Address</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Hello World</td>
<td>Some Where</td>
</tr>
<tr>
<td>2</td>
<td>Testing</td>
<td>Some Where</td>
</tr>
</tbody>
</table>
有没有办法匹配除&#34; {{&#34;
以外的所有内容答案 0 :(得分:1)
您的描述含糊不清。我不认为这正是你所追求的,但也许它会有所帮助。
val pttrn = """(\{|\{\{\{+)"""
"{" matches pttrn //true
"{{" matches pttrn //false
"{{{" matches pttrn //true
"{{{{" matches pttrn //true
答案 1 :(得分:0)
如果我正确理解您的要求,您可以在正则表达式模式中附加{{
字边界\b
:
val pattern = """^(.*?)\b\{\{\b.*$""".r
val str = "{a}bb{{{cc}}}ddd{{e}}ff{{{{gg}}}}"
str match {
case pattern(s) => s
case _ => "no-match"
}
// res1: String = {a}bb{{{cc}}}ddd
答案 2 :(得分:0)
我想出了这个:
val samples = List ("foo{bar", "foo{{bar", "foo{{{bar", "{foo{bar{{baz{{{fobar{", "{{foo{bar{{baz{{{fobar{{")
samples.map (_.replaceAll ("(^|[^{])\\{{2}([^{]|$)", "##"))
res62: List[String] = List(foo{bar, fo##ar, foo{{{bar, {foo{ba##az{{{fobar{, ##oo{ba##az{{{foba##)
它取代了可以存活的模式,但我想你可以要求pattern.matches(...),如果没有,删除。
答案 3 :(得分:-1)
匹配所有内容,包括&#34; {&#34;,&#34; {{{&#34;,以及匹配停靠时到达&#34; {{&#34;,您可以使用积极的后视batch-cluster
和积极的前瞻(?<
:
^.+?(?:(?<=(?:[^{]|^)\{\{)(?=[^{])|$)
这将匹配
^ # Assert position at start of line .+? # Match any character one or more times non greedy (?: # Non capturing group (?<= # Positive lookbehind that asserts that what is before (?: # Non capturing group [^{] # Match not { | # Or ^ # Assert position at start of line ) # Close non capturing group [^{]\{\{ # Match not {, match {{ ) # Close lookbehind (?= # Positive lookahead [^{] # Match not { ) # Close lookahead | # Or $ # Assert position at the end of the line ) # Close non capturing group
(?=
会导致:
测试{和{{{和{{