SHARE
TWEET

Untitled

shniaga Mar 27th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package TextAndRegexMoreExercise;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class ExtractPersonInformation_01 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int lines = Integer.parseInt(scanner.nextLine());
  12.         String nameRegex = "(?<=@)[A-Za-z]+(?=\\|)";
  13.         String ageRegex = "(?<=#)[\\d]+(?=\\*)";
  14.         Pattern namePattern = Pattern.compile(nameRegex);
  15.         Pattern agePattern = Pattern.compile(ageRegex);
  16.  
  17.  
  18.         for (int i = 0; i < lines; i++) {
  19.             String name = "";
  20.             String age = "";
  21.             String text = scanner.nextLine();
  22.             Matcher nameMatcher = namePattern.matcher(text);
  23.             Matcher ageMatcher = agePattern.matcher(text);
  24.             if (nameMatcher.find()){
  25.                 name = nameMatcher.group();
  26.             }
  27.             if (ageMatcher.find()){
  28.                 age = ageMatcher.group();
  29.             }
  30.             System.out.println(String.format("%s is %s years old.",name,age));
  31.         }
  32.  
  33.  
  34.     }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top