crockpotveggies
crockpotveggies

Reputation: 13320

Necessary to escape a java regular expression in matches()?

I'm currently doing a test on an HTTP Origin to determine if it came from SSL:

(HttpHeaders.Names.ORIGIN).matches("/^https:\\/\\//")

But I'm finding it's not working. Do I need to escape matches() strings like a regular expression or can I leave it like https://? Is there any way to do a simple string match?

Seems like it would be a simple question, but surprisingly I'm not getting anywhere even after using a RegEx tester http://www.regexplanet.com/advanced/java/index.html. Thanks.

Upvotes: 1

Views: 97

Answers (2)

Adolfo Perez
Adolfo Perez

Reputation: 2874

How about this Regex:

"^(https:)\/\/.*" 

It works in your tester

Upvotes: 0

Bart Kiers
Bart Kiers

Reputation: 170207

Java's regex doesn't need delimiters. Simply do:

.matches("https://.*")

Note that matches validates the entire input string, hence the .* at the end. And if the input contains line break chars (which . will not match), enable DOT-ALL:

.matches("(?s)https://.*")

Of couse, you could also simply do:

.startsWith("https://")

which takes a plain string (no regex pattern).

Upvotes: 3

Related Questions