Match IP addresses using regular expressions in Java

Overview

Since it was necessary to add a validation function for IP addresses and network settings using java, I implemented it using regular expressions. It is the memorandum.

How to use regular expressions in java

private boolean validateHoge(string target) {
    Pattern pattern = Pattern.compile("[Write a regular expression here]");
    final Matcher matcher = pattern.matcher(target);

    return matcher.matches();
}

You can now use regular expressions in java. matches () checks if the entire string matches, so use find () to check if part of the string matches the pattern.

IP address regular expression

// 0.0.0.0 ~ 255.255.255.Up to 255
Pattern pattern = Pattern.compile("^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");

It's straightforward, but I tried to express it like this.

Regular expressions I wrote incidentally

// 1 ~Up to 32
Pattern pattern = Pattern.compile("^([1-9]|[1-2][0-9]|3[0-1](.[0-9]+)?)|32$");

(I realized that it would be easier to change it to Integer after writing and limit it)

// 0~9, a~z, A~z, "."
Pattern pattern = Pattern.compile("[0-9a-zA-z.]+?");

A site where you can test regular expressions in java on your browser

https://regex-testdrive.com/ja/dotest

This site was very convenient. It is useful when creating regular expressions because it shows the results for each method with matches () find ().

Recommended Posts

Match IP addresses using regular expressions in Java
Regular expressions that match 99% of email addresses
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
Notes on how to use regular expressions in Java
Try using RocksDB in Java
[Java] Summary of regular expressions
About regular expressions in Ruby
Rails: Capture regular expressions in emails!
Encrypt using RSA cryptography in Java
Parse Japanese addresses with regular expressions
Implementation of validation using regular expressions
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Regular expressions
I tried using Elasticsearch API in Java
Name a group of regular expressions (Java)
Try using the Stream API in Java
Map without using an array in java
Using JavaScript from Java in Rhino 2021 version
ERRORCODE = -4471 occurs in Java application using Db2.
Try using JSON format API in Java
Read Felica using RC-S380 (PaSoRi) in Java
Easy to trip with Java regular expressions
ChatWork4j for using the ChatWork API in Java
[Java] API creation using Jerjey (Jax-rs) in eclipse
Send email using Amazon SES SMTP in Java
Send push notifications using Notification Hubs in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java
NLP4J [001b] Morphological analysis in Java (using kuromoji)
About regular expressions used in ruby sub method
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
[Java] Sort the list using streams and lambda expressions
How to convert A to a and a to A using AND and OR in Java
I tried using an extended for statement in Java
Try global hooking in Java using the JNativeHook library
Differences in code when using the length system in Java