Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Java/Strings/JavaRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@

*/


class myRegex
{
String pattern = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

}
import java.util.Scanner;

class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String IP = in.next();
boolean isValid = IP.matches(new MyRegex().pattern);
System.out.println(isValid);
}
}
}

class MyRegex {
String num = "([01]?\\d{1,2}|2[0-4]\\d|25[0-5])";

// Construct the pattern using the defined numeric segment
String pattern = num + "." + num + "." + num + "." + num;
}