-1

I have to build smth with URL, but only with valid google play links. I will get a link from user and want to check it before connect to it. If i have a Google Play link like this: https://play.google.com/store/apps/details?id=com.... I want to match it with regex and method matches (or should I just use method contains?) If link contains play.google.com - it's true and then do some actions. If false - throw new exception.

I tried with

\\. + play.google. + \\. 

but it doesn't work at all.

dogyears
  • 57
  • 4
  • Does this answer your question? [Regular expression to match URLs in Java](https://stackoverflow.com/questions/163360/regular-expression-to-match-urls-in-java) – Renato Feb 16 '20 at 19:50
  • 2
    Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Feb 16 '20 at 20:56

1 Answers1

1

\. matches a literal, actual dot. You want the actual dot:

Pattern.compile("^.*play\\.google\\..*$");

However, you do not want to do this – here is a URL that will match: https://www.hacker.ru/hahaThisWillMatch?irrelevant=play.google

Put the URI into the java.net.URI class, and ask it what the server is, as well as the port, and the protocol. The port needs to be non-existent, the protocol needs to be https, and the server needs to be play.google.com, I'm not sure what other google domains you're looking for, but if you are, check against them all, don't try to use a regexp, because there are thousands of top level domains and google didn't register 'google' in literally ALL of them.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72