简体   繁体   English

文本块内的 Java 正则表达式

[英]Java regex inside text blocks

I surely hoped that this would be supported:我当然希望这会得到支持:

private static void regex() {
    String plain = "\\w+";
    String withTextBlocks = """
        \w+
    """;
}

but withTextBlocks does not compile under Java-17.withTextBlocks不能在 Java-17 下编译。 Isn't it the point of text blocks that we should not escape?不是我们不应该逃避的文本块的重点吗? I have been through the JEP and maybe the explanation is there, but I can't grok through it.我已经通过JEP ,也许解释就在那里,但我无法理解它。 And a second question in case someone knows, is there a future JEP for this?如果有人知道,第二个问题是,未来有 JEP 吗? Thank you.谢谢你。

PS It works in Kotlin. PS 它适用于 Kotlin。

You are conflating text blocks with raw strings .您正在将文本块原始字符串混为一谈。 These are different features, though they were explored together and this may explain why you mentally folded them together.这些是不同的特征,尽管它们是一起探索的,这可以解释为什么你在心理上将它们折叠在一起。 There is no support yet for raw strings (which turn out to be somewhat more slippery than they might first appear.)尚不支持原始字符串(事实证明它比最初出现的要复杂一些。)

Isn't it the point of text blocks that we should not escape?不是我们不应该逃避的文本块的重点吗?

No, that is not the point of text blocks.不,这不是文本块的重点。 The point of text blocks is to allow us to represent two dimensional blocks of text in code, preserving the block's relative indentation but not absolute indentation.文本块的重点是允许我们在代码中表示二维文本块,保留块的相对缩进而不是绝对缩进。 This allows us to freely indent the source representation of the text block itself to match surrounding code, without affecting the indentation of the string the text block describes.这允许我们自由缩进文本块本身的源表示以匹配周围的代码,而不会影响文本块描述的字符串的缩进。

An additional design goal is that text blocks should differ from ordinary string literals only in ways that pertain to their two-dimensional nature.另一个设计目标是文本块与普通字符串文字的区别仅在于它们的二维性质。 There should not be a different set of escape characters, or different escaping rules.不应有不同的转义字符集或不同的 escaping 规则。 (If we ever do raw strings, it should apply equally to text blocks and traditional string literals.) If text blocks worked the way you wanted, you'd probably be complaining that you can't do the same with single-line strings. (如果我们曾经做过原始字符串,它应该同样适用于文本块和传统的字符串文字。)如果文本块按照您想要的方式工作,您可能会抱怨您不能对单行字符串做同样的事情。 These aspects are orthogonal and the language should treat them orthogonally.这些方面是正交的,语言应该正交对待它们。

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

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