Skip to content

Instantly share code, notes, and snippets.

@rmxsantiago
Created August 27, 2017 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmxsantiago/b6648a4657f25a98d593ccf9177badac to your computer and use it in GitHub Desktop.
Save rmxsantiago/b6648a4657f25a98d593ccf9177badac to your computer and use it in GitHub Desktop.
Regex which return content from each group
package com.company;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Main main = new Main();
String input = "[0-9]{1,13}";
String regex = "\\[.*]\\{([0-9]+),([0-9]+)}";
Pattern pattern = Pattern.compile(regex);
try {
Matcher matcher = pattern.matcher(input);
main.print(input, matcher);
}catch (Exception e){
e.printStackTrace();
}
}
public void print(String input, Matcher matcher){
System.out.println("Input: " + input);
System.out.println("Matches? " + matcher.matches());
System.out.println("Groups: " + matcher.groupCount());
System.out.println("Min: " + matcher.group(1));
System.out.println("Max: " + matcher.group(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment