1

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.

3
  • can you define but don't work can you share some inputs that not works ok please Commented May 15, 2018 at 17:33
  • Why doesn't it work? What is your input? Which method are you using? Please post a minimal reproducible example Commented May 15, 2018 at 17:34
  • The error is no in the input, the error is in the regex java syntax, the question is Why its happening only in it? I write wrong or PHP and JAVA have different manipulation? Commented May 15, 2018 at 18:22

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

There's no need for them, but they shouldn't cause it to fail, should it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.