thufir

digitsOnly

Sep 2nd, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package regex;
  2.  
  3. import java.util.AbstractMap;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class Regex {
  11.  
  12.     public static void main(String[] args) {
  13. //        String telnetText = "HP: 648/648  CP: 657/657  ADRENALINE: 105 ENDORPHINE: 0  BERSERK: 10 None: 0% [Darts: 0][Blood: 69068][Grafts: 0/0][Co][En]";
  14.         String telnetText = "HP: 648/648  CP: 657/657  ADRENALINE: 105 ENDORPHINE: 0  BERSERK: 10";
  15.         String keyName = null;
  16.         String keyVal = null;
  17.         String digitsOnly;
  18.         try {
  19.             Pattern pattern = Pattern.compile("(\\w+): (\\S+)");
  20.             Matcher matcher = pattern.matcher(telnetText);
  21.             List<Map.Entry> stringEntries = new ArrayList<>();
  22.             while (matcher.find()) {
  23.                 keyName = matcher.group(1);
  24.                 keyVal = matcher.group(2);
  25.                 Map.Entry<String, String> entr = new AbstractMap.SimpleEntry<>(keyName, keyVal);
  26.                 stringEntries.add(entr);
  27.             }
  28.  
  29.             //do something interesting with the data... make Map.Entry<String,Integer> first
  30.  
  31.             Pattern p = Pattern.compile("(\\d+)`");
  32.             Matcher m;
  33.             for (int i = 0; i < stringEntries.size(); i++) {
  34.                 Map.Entry<String, String> e = stringEntries.remove(i);
  35.                 System.out.println(e.getKey() + e.getValue() + "\n*******\n");
  36.                 m = p.matcher(keyVal);
  37.                 while (m.find()) {
  38.                     digitsOnly = m.group(1);
  39.                     System.out.println(e.getKey() + digitsOnly);
  40.                 }
  41.             }
  42.         } catch (IllegalStateException e) {
  43.         }
  44.  
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment