-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавлены методы is*() и find*() для локализации, IP адресов и адресо…
…в электронной почты
- Loading branch information
1 parent
bc0df8b
commit 79aea7e
Showing
12 changed files
with
845 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,117 @@ | ||
# StringRegexp | ||
|
||
Реализации регулярных выражений для строк. | ||
Операции над строками с помощью регулярных выражений. | ||
|
||
## Использование | ||
|
||
#### Заголовок | ||
### Проверка | ||
|
||
#### Проверка локализации | ||
|
||
```java | ||
StringRegexp.isLocale("ru-RU"); | ||
// Результат выполнения: true | ||
``` | ||
|
||
#### Проверка адреса электронной почты | ||
|
||
```java | ||
StringRegexp.isEmailAddress("[email protected]"); | ||
// Результат выполнения: true | ||
``` | ||
|
||
#### Проверка IP адреса четвёртой версии | ||
|
||
```java | ||
StringRegexp.isInet4Address("192.168.1.1"); | ||
// Результат выполнения: true | ||
``` | ||
|
||
#### Проверка IP адреса шестой версии | ||
|
||
```java | ||
StringRegexp.isInet6Address("EF98:3:0:0:0:0:2F3B:7654"); | ||
// Результат выполнения: true | ||
``` | ||
|
||
### Поиск | ||
|
||
#### Поиск локализаций | ||
|
||
```java | ||
String text = """ | ||
Застоялся мой поезд в депо. | ||
Снова я by-BY уезжаю. Пора… | ||
На пороге ветер заждался меня. | ||
На пороге осень — моя сестра.ru-RU | ||
"""; | ||
|
||
StringRegexp.findLocale(text); | ||
/* | ||
Результат выполнения: | ||
{ | ||
by-BY, | ||
ru-RU | ||
} | ||
*/ | ||
``` | ||
|
||
#### Поиск адресов электронной почты | ||
|
||
```java | ||
String text = """ | ||
И я должен прийти к девяти, на работу свою, [email protected] | ||
Но сейчас уже без десяти, а я только встаю | ||
На столе моем [email protected] завтрак стоит, от него не уйти | ||
И наверное, я к девяти не смогу подойти | ||
"""; | ||
|
||
StringRegexp.findEmailAddress(text); | ||
/* | ||
Результат выполнения: | ||
{ | ||
[email protected], | ||
[email protected] | ||
} | ||
*/ | ||
``` | ||
|
||
#### Поиск IP адресов четвёртой версии | ||
|
||
```java | ||
String text = """ | ||
День как день,10.222.170.80 только ты почему-то грустишь. | ||
И вокруг все поют, только ты один молчишь. | ||
Потерял аппетит и не 192.168.1.1 хочешь сходить в кино. | ||
Ты идешь в магазин, чтобы купить вино. | ||
"""; | ||
|
||
StringRegexp.findInet4Address(text); | ||
/* | ||
Результат выполнения: | ||
Результат выполнения: | ||
{ | ||
10.222.170.80, | ||
192.168.1.1 | ||
} | ||
*/ | ||
``` | ||
|
||
#### Поиск IP адресов шестой версии | ||
|
||
```java | ||
String text = """ | ||
В последнее время я редко был дома, | ||
Так ad12::ae21:: что даже отвыкли звонить мне друзья. | ||
В разъездах, разгулах 2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d конца лета симптомы | ||
Совсем перестали вдруг мучить меня. | ||
"""; | ||
|
||
StringRegexp.findInet6Address(text); | ||
/* | ||
Результат выполнения: | ||
{ | ||
ad12::ae21::, | ||
2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d | ||
} | ||
*/ | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,22 @@ | |
* SOFTWARE. | ||
*/ | ||
|
||
package dev.kalenchukov.string.regexp.validation; | ||
package dev.kalenchukov.string.regexp; | ||
|
||
public class Main | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
String text = """ | ||
by-BY dsgdfg dfg dfgh ru-RU gfdfg dfg dfgh fdhg dfg h | ||
dfhg fgh | ||
gdfhgf gh [email protected] | ||
en-EN dfgfhg | ||
dfgdfg | ||
dfhg | ||
"""; | ||
|
||
System.out.println(StringRegexp.findLocale(text)); | ||
System.out.println(StringRegexp.findEmailAddress(text)); | ||
} | ||
} |
176 changes: 176 additions & 0 deletions
176
src/main/java/dev/kalenchukov/string/regexp/StringRegexp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.kalenchukov.string.regexp; | ||
|
||
import dev.kalenchukov.string.regexp.resources.Regexp; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Класс содержит статические методы для проверки корректности строк с помощью регулярных выражений. | ||
*/ | ||
public class StringRegexp | ||
{ | ||
/** | ||
* Конструктор для {@code StringRegexp} запрещающий создавать объект класса. | ||
*/ | ||
private StringRegexp() {} | ||
|
||
/** | ||
* Проверяет, является ли строка локализацией. | ||
* | ||
* @param string Строка | ||
* @return {@code True}, если строка является локализацией, иначе {@code false}. | ||
*/ | ||
public static boolean isLocale(@NotNull final String string) | ||
{ | ||
return StringRegexp.is(string, Regexp.LOCALE); | ||
} | ||
|
||
/** | ||
* Выполняет поиск локализаций. | ||
* | ||
* @param string Строка. | ||
* @return Коллекцию с найденными локализациями. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
public static List<@NotNull String> findLocale(@NotNull final String string) | ||
{ | ||
return StringRegexp.find(string, Regexp.LOCALE); | ||
} | ||
|
||
/** | ||
* Проверяет, является ли строка IP адресом четвёртой версии. | ||
* | ||
* @param string Строка | ||
* @return {@code True}, если строка является IP адресом четвёртой версии, иначе {@code false}. | ||
*/ | ||
public static boolean isInet4Address(@NotNull final String string) | ||
{ | ||
return StringRegexp.is(string, Regexp.INET_4_ADDRESS); | ||
} | ||
|
||
/** | ||
* Выполняет поиск IP адресов четвёртой версии. | ||
* | ||
* @param string Строка. | ||
* @return Коллекцию с найденными IP адресами четвёртой версии. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
public static List<@NotNull String> findInet4Address(@NotNull final String string) | ||
{ | ||
return StringRegexp.find(string, Regexp.INET_4_ADDRESS); | ||
} | ||
|
||
/** | ||
* Проверяет, является ли строка IP адресом шестой версии. | ||
* | ||
* @param string Строка | ||
* @return {@code True}, если строка является IP адресом шестой версии, иначе {@code false}. | ||
*/ | ||
public static boolean isInet6Address(@NotNull final String string) | ||
{ | ||
return StringRegexp.is(string, Regexp.INET_6_ADDRESS); | ||
} | ||
|
||
/** | ||
* Выполняет поиск IP адресов шестой версии. | ||
* | ||
* @param string Строка. | ||
* @return Коллекцию с найденными IP адресами шестой версии. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
public static List<@NotNull String> findInet6Address(@NotNull final String string) | ||
{ | ||
return StringRegexp.find(string, Regexp.INET_6_ADDRESS); | ||
} | ||
|
||
/** | ||
* Проверяет, является ли строка адресом электронной почты. | ||
* | ||
* @param string Строка | ||
* @return {@code True}, если строка является адресом электронной почты, иначе {@code false}. | ||
*/ | ||
public static boolean isEmailAddress(@NotNull final String string) | ||
{ | ||
return StringRegexp.is(string, Regexp.EMAIL_ADDRESS); | ||
} | ||
|
||
/** | ||
* Выполняет поиск адресов электронной почты. | ||
* | ||
* @param string Строка. | ||
* @return Коллекцию с найденными адресами электронной почты. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
public static List<@NotNull String> findEmailAddress(@NotNull final String string) | ||
{ | ||
return StringRegexp.find(string, Regexp.EMAIL_ADDRESS); | ||
} | ||
|
||
/** | ||
* Проверяет, является ли строка значением. | ||
* | ||
* @param regexp Регулярное выражение. | ||
* @param string Строка. | ||
* @return {@code True}, если строка является значением, иначе {@code false}. | ||
*/ | ||
private static boolean is(@NotNull final String string, @NotNull final Regexp regexp) | ||
{ | ||
return string.matches("^" + regexp.getPattern() + "$"); | ||
} | ||
|
||
/** | ||
* Выполняет поиск значений. | ||
* | ||
* @param string Строка. | ||
* @return Коллекцию с найденными значениями. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
private static List<@NotNull String> find(@NotNull final String string, @NotNull final Regexp regexp) | ||
{ | ||
final List<String> locales = new ArrayList<>(); | ||
|
||
final Pattern pattern = Pattern.compile(regexp.getPattern()); | ||
final Matcher matcher = pattern.matcher(string); | ||
|
||
while (matcher.find()) { | ||
locales.add(matcher.group(regexp.getGroup())); | ||
} | ||
|
||
return Collections.unmodifiableList(locales); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
src/main/java/dev/kalenchukov/string/regexp/parsing/StringParser.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.