简体   繁体   English

如何检查给定的Java Regex在Python中是否有效

[英]How to check if a given Java Regex is valid in Python

I want to check if the Regex defined in Java is valid by Python. 我想检查Java中定义的Regex是否对Python有效。

But the Regex in the two languages are little different. 但是两种语言的Regex几乎没有什么不同。

For example, to parse the character dot ( . ) 例如,解析字符点( .

"\."  # Python version
"\\." # Java version

Is there any way to check Java regex in Python? 有什么方法可以在Python中检查Java正则表达式吗?

Regex are the same in java and python. 正则表达式在java和python中是相同的。 The difference you pointed out is due to the way java compiler handles string constants. 您指出的差异是由于Java编译器处理字符串常量的方式。 Backslash has special meaning as an escape character, so, to include a backslash itself into a String literal, you have to repeat it twice. 反斜杠作为转义字符具有特殊含义,因此,要将反斜杠本身包含在String文字中,您必须重复两次。 Thus "\\\\." 因此为"\\\\." to express a string constant "\\." 表示字符串常量"\\."

Try this: System.out.println(Pattern.compile("\\\\.").toString()); 试试这个: System.out.println(Pattern.compile("\\\\.").toString());

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

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