Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package regex;
- import java.util.AbstractMap;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Regex {
- public static void main(String[] args) {
- // 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]";
- String telnetText = "HP: 648/648 CP: 657/657 ADRENALINE: 105 ENDORPHINE: 0 BERSERK: 10";
- String keyName = null;
- String keyVal = null;
- String digitsOnly;
- try {
- Pattern pattern = Pattern.compile("(\\w+): (\\S+)");
- Matcher matcher = pattern.matcher(telnetText);
- List<Map.Entry> stringEntries = new ArrayList<>();
- while (matcher.find()) {
- keyName = matcher.group(1);
- keyVal = matcher.group(2);
- Map.Entry<String, String> entr = new AbstractMap.SimpleEntry<>(keyName, keyVal);
- stringEntries.add(entr);
- }
- //do something interesting with the data... make Map.Entry<String,Integer> first
- Pattern p = Pattern.compile("(\\d+)`");
- Matcher m;
- for (int i = 0; i < stringEntries.size(); i++) {
- Map.Entry<String, String> e = stringEntries.remove(i);
- System.out.println(e.getKey() + e.getValue() + "\n*******\n");
- m = p.matcher(keyVal);
- while (m.find()) {
- digitsOnly = m.group(1);
- System.out.println(e.getKey() + digitsOnly);
- }
- }
- } catch (IllegalStateException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment