Advertisement
desislava_topuzakova

01. Match Full Name

Nov 17th, 2022
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. package regex;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class MatchFullName_01 {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. String text = scanner.nextLine();
  11. //"Ivan Ivanov, Ivan ivanov, ivan Ivanov, IVan Ivanov, Georgi Georgiev, Ivan Ivanov"
  12. String regex = "\\b[A-Z][a-z]+ [A-Z][a-z]+\\b";
  13. Pattern pattern = Pattern.compile(regex); // шаблон
  14.  
  15. Matcher matcher = pattern.matcher(text); //текстовете от променливата text, които отговарят на шаблона
  16. //matcher = {Ivan Ivanov, Georgi Georgiev}
  17.  
  18. while (matcher.find()) {
  19. System.out.print(matcher.group() + " ");
  20. }
  21. }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement