Skip to content

Instantly share code, notes, and snippets.

@pbesra
Last active January 9, 2017 07:40
Show Gist options
  • Save pbesra/43d267eb214ae727d4fc77ad75bb7281 to your computer and use it in GitHub Desktop.
Save pbesra/43d267eb214ae727d4fc77ad75bb7281 to your computer and use it in GitHub Desktop.
Regex in Java (Pattern, Matcher)
//----------------------------------code 1-----------------------------
package testPackage;
import java.util.*;
import java.util.regex.Pattern;
import jdk.internal.org.objectweb.asm.commons.TryCatchBlockSorter;
import java.io.*;
import java.lang.*;
import java.math.*;
class Test{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String IP=input.nextLine();
System.out.println(IP.matches(new MyRegex().pattern));
// IP address example
// 000.12.12.234
//ip==A.B.C.D, A.length<=3, A<=255 and all are numbers
}
}
class MyRegex{
String zero="([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";
String pattern=zero+"."+zero+"."+zero+"."+zero;
}
//----------------------------------code 2-----------------------------------------
/*
1) \\d{1,2} catches any one or two digit number
2) (0|1)\\d{2} catches any three digit number starting with 0 or 1.
3) 2[0-4]\\d catches numbers between 200 and 249.
4) 25[0-5] catches numbers between 250 and 255.
*/
/*
1-9, 01-09, 10-99 --> \d{1,2}
000-009, 010-099, 100-199 --> {0|1}\d{2}
200-249 --> 2[0-4]\d
250-255 --> 25[0-5]
\d <=> [0-9]
*/
/*
More on Rgex :
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.Query;
import java.lang.*;
import java.lang.*;
import java.lang.*;
import java.awt.List;
import java.io.*;
import java.time.*;
import java.math.*;
import java.rmi.Remote;
import java.sql.Array;
//demostration of Pattern and Matcher in java
public class MyClass {
public static void main(String[] args)throws java.lang.Exception{
//Scanner input=new Scanner(System.in);
//1
System.out.println(Pattern.matches("ge*pra*kas*hw", "geekpraakashw"));
//2
Pattern pat=Pattern.compile("geeks");
Matcher m=pat.matcher("geeksforgeeks");
while(m.find()){
System.out.println("pattern found from "+m.start()+" to "+ m.end());
}
//3. Java program to demonstrate Case Insensitive Searching
Pattern p=Pattern.compile("ge*", Pattern.CASE_INSENSITIVE);
Matcher m1=p.matcher("geeksforgeeks.org");
while(m1.find()){
System.out.println("pattern found from +"+m1.start()+" to "+m1.end());
}
//4. Java program to demonstrate working of split() to split a text based on a delimiter pattern
String text="geeks1for2geeks3web";
String delim="\\d";
String[] s=text.split(delim);
for(String ans : s){
System.out.println(ans);
}
}
}
//-----------------------------------code 3-------------------------------------------------
*/
/*
// HTML Extractor using Regex (although, it's not suggested, however)
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.Query;
import java.lang.*;
import java.lang.*;
import java.lang.*;
import java.awt.List;
import java.io.*;
import java.time.*;
import java.math.*;
import java.rmi.Remote;
import java.sql.Array;
//demostration of Pattern and Matcher in java
public class MyClass {
public static void main(String[] args)throws java.lang.Exception{
Pattern r=Pattern.compile("<(.+?)>([^<>]+)</\\1>");
Scanner input=new Scanner(System.in);
int test=Integer.parseInt(input.nextLine());
int count=0;
while(test-- >0){
count=0;
String line=input.nextLine();
Matcher m=r.matcher(line);
while(m.find()){
System.out.println(m.group(2));
count++;
}
if(count==0){
System.out.println("None");
}
}
}
}
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment