user2022323
user2022323

Reputation: 35

Translate php regex to java

I have trouble to translate this php regex /^([-\.\w]+)$/ to java regex.

I try ^([-\\.\\w]+)$ but don't work.

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.

Upvotes: 1

Views: 80

Answers (1)

Bohemian
Bohemian

Reputation: 425063

In java, it would be:

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)
  • There is no need to create a group (the brackets)

Upvotes: 2

Related Questions