regex 在Ballerina正则表达式中使用转义符[重复]

cgvd09ve  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

此问题已在此处有答案

Java doesn't work with regex \s, says: invalid escape sequence(3个答案)
25天前关闭

public function main() {
    boolean matches = regex:matches("ABCdef", "^(?!\s*$).+");
}

在上面的代码片段中,我们使用了带有转义字符的正则表达式。编译代码时,会抛出如下所示的编译错误。

invalid escape sequence '\s'

如何解决此问题?

flvlnr44

flvlnr441#

为了转义,我们必须使用\\而不是\
因此正确的代码是,

import ballerina/regex;

public function main() {
    boolean matches = regex:matches("ABCdef","^(?!\\s*$).+");
}

请查看Ballerina regex文档https://lib.ballerina.io/ballerina/regex/1.3.0#matches了解更多详细信息。

相关问题