简体   繁体   English

将php regex转换为Java

[英]Translate php regex to java

I have trouble to translate this php regex /^([-\\.\\w]+)$/ to java regex. 我很难将此php regex /^([-\\.\\w]+)$/为Java regex。

I try ^([-\\\\.\\\\w]+)$ but don't work. 我尝试^([-\\\\.\\\\w]+)$但不起作用。

The regex is used to validate a string used for a name of file. 正则表达式用于验证用于文件名的字符串。

in PHP is not allowed têst.ext , but in JAVA it's. 在PHP中不允许têst.ext ,但在JAVA中是允许的。

In java, it would be: 在Java中,它将是:

str.matches("[-.\\w]+")
  • There is no need to escape the dot in a character class in any language/tool. 无需使用任何语言/工具在字符类中转义点。
  • There is no need to use ^ or $ with java's String#matches() because it's implied (the whole string must match) 无需将^$与Java的String#matches()因为这是隐式的( 整个字符串必须匹配)
  • There is no need to create a group (the brackets) 无需创建组(方括号)

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

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