Created
March 23, 2018 08:14
-
-
Save webstory/08c76c94996f570266d883c5429f03a3 to your computer and use it in GitHub Desktop.
Java regex example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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