1

I have a problem with java regex. I have something like

com/products/id/response.html

And I want the id as anything from Aa-Zz 0-9, I have tried:

com/products/A-Za-z0-9]+/response.html

However with no success, but in this case

com/products/A-Za-z0-9]+.html

It works just fine... Any idea where Am I making mistake?

2 Answers 2

1
  • Character class is [...] and you have missing [
  • You need to escape dot
  • Jave needs \\ for escaping

You can use this regex:

com/products/[A-Za-z0-9]+/response\\.html
  • You may need to use anchors ^ and $ also.
Sign up to request clarification or add additional context in comments.

5 Comments

I tend to do [.] so I don't shoot myself in the foot with \\.
Also, if you try to use this regex in sed, Javascript, or Perl, the forward slashes need escaping, too.
Yes that's correct about Javascript but you can use an alternative delimiter in Perl/sed to avoid escaping in substitute commands.
Thank you for pointing me out, however it still doesn't seem to work
I have tested is separately and It works, seems like there is something wrong with my other components that make this fail. Thank you for answer
0

You missed the first parentheses, so it need to look like this:

com/products/[A-Za-z0-9]+/response[.]html

Comments

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.