Advertisement
Guest User

ExtractPersonInformation

a guest
Nov 19th, 2021
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.  
  2. import java.util.regex.MatchResult;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7. import java.util.stream.Stream;
  8.  
  9. public class Test {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         int n = Integer.parseInt(scanner.nextLine());
  15.  
  16.         for (int i = 1; i <= n; i++) {
  17.             String text = scanner.nextLine();
  18.             String regexName = "@(?<name>[A-Za-z]+)\\|";
  19.             String regexAge = "#(?<age>[0-9]+)\\*";
  20.  
  21.             Pattern patternName = Pattern.compile(regexName);
  22.             Matcher matcherName = patternName.matcher(text);
  23.             Pattern patternAge = Pattern.compile(regexAge);
  24.             Matcher matcherAge = patternAge.matcher(text);
  25.  
  26.  
  27.             while (matcherName.find() && matcherAge.find()) {
  28.                 String name = matcherName.group("name");
  29.                 String age = matcherName.group("age");
  30.  
  31.                 System.out.printf("%s is %s years old.",name,age);
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement