digital-garden/knowledge/dev/java/snippets/Парсинг URL c помощью регулярки.md
2024-06-13 21:01:37 +03:00

26 lines
725 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
tags:
- зрелость/🌱
date:
- - 2023-11-20
zero-link:
- "[[00 Java разработка]]"
parents:
- "[[Снипеты на Java]]"
linked:
article: https://note.struchkov.dev/parsingh-url-c-pomoshchiu-rieghuliarki/
---
Разделение URL-адреса на протокол, домен, порт и URI с помощью регулярных выражений.
```java
// Split URL into protocol, domain, port and URI
Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
Matcher matcher = pattern.matcher(url);
matcher.find();
String protocol = matcher.group(1);
String domain = matcher.group(2);
String port = matcher.group(3);
String uri = matcher.group(4);
```