Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2017 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/08d90a78656143f8c1a2f16419f2bb1f to your computer and use it in GitHub Desktop.
Save anonymous/08d90a78656143f8c1a2f16419f2bb1f to your computer and use it in GitHub Desktop.
null created by devcarlos - https://repl.it/GkZr/7
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args){
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern =
Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher =
pattern.matcher(console.readLine("Enter input string to search: "));
boolean found = false;
while (matcher.find()) {
console.format("I found the text" +
" \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if(!found){
console.format("No match found.%n");
}
}
}
}
@Carlos-Alonzo
Copy link

This is a simple sample code of java regular expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment