Skip to content

Instantly share code, notes, and snippets.

@webstory
Created March 23, 2018 08:14
Show Gist options
  • Save webstory/08c76c94996f570266d883c5429f03a3 to your computer and use it in GitHub Desktop.
Save webstory/08c76c94996f570266d883c5429f03a3 to your computer and use it in GitHub Desktop.
Java regex example
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Main {
public static void main(String[] args) {
String s = "여기는.어디.입니까-12";
Pattern p = Pattern.compile("^([^.]+\\.[^.]+\\.[^-.]+)(?:-([0-9]{2}))?$");
Matcher m = p.matcher(s);
if(m.find()) {
String wordonly = m.group(1);
String cellNum = m.group(2);
System.out.println(wordonly);
if(cellNum != null) { System.out.println(cellNum); }
else { System.out.println("44"); }
} else {
System.out.println("Malformed 3word");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment