正则获取一组数据

 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Perl5Substitution;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Util;

public
class regexUtils { private final static String regIp = "ip:([^,]+)"; /** * @param str * @param reg * @return * @throws MalformedPatternException */ public static DynamicArray<String> getHostData(String str, String reg) throws MalformedPatternException { DynamicArray<String> tempArray = new DynamicArray<String>(); List<MatchResult> match = new ArrayList<MatchResult>(); regexUtils.getMatchResults(match, str, reg); for (int i = 0; i < match.size(); i++) { String ip = match.get(i).group(1); tempArray.add(ip); } return tempArray; } /** * return match array * @param results * @param pageContent * @param regStr * @throws MalformedPatternException */ public static void getMatchResults(List<MatchResult> results, String pageContent, String regStr) throws MalformedPatternException { PatternCompiler compiler = new Perl5Compiler(); Pattern reg = compiler.compile(regStr, Perl5Compiler.CASE_INSENSITIVE_MASK); Perl5Matcher matcher = new Perl5Matcher(); Perl5Substitution substitution = new Perl5Substitution(); while (matcher.contains(pageContent, reg)) { substitution.setSubstitution(""); results.add(matcher.getMatch()); pageContent = Util.substitute(matcher, reg, substitution, pageContent); } } public static vodi main(String[] args){ String sourceData = "{172.16.134.134=SystemPerformanceEntity[ ip: 172.16.134.134, cpuUsage: 5.9%, memoryUsage: 28.555202535499337%, load: 0.99, diskUsage: {/sys/fs/cgroup=3%, /dev=4%, /run/shm=5%, /run=6%, /run/lock=7%}], 172.16.134.131=SystemPerformanceEntity[ ip: 172.16.134.131, cpuUsage: 5.0%, memoryUsage: 28.52202558918353%, load: 0.41, diskUsage: {/sys/fs/cgroup=0%, /dev=1%, /run/shm=1%, /run=1%, /run/lock=1%}]}"; DynamicArray<String> ips = regexUtils.getHostData(sourceData , regIp); System.out.println(ips); } }

 

动态数组类:

public class DynamicArray<AnyType> {
    private int size = 0;
    private AnyType theItem[] = null;

    public DynamicArray(AnyType a[]) {
        this.theItem = a;
        this.size = a.length;
    }

    public DynamicArray() {
    }

    public int getSize() {
        return this.size;
    }

    public AnyType get(int nIndex) {
        return this.theItem[nIndex];
    }

    public void Set(int nIndex, AnyType newElement) {
        this.theItem[nIndex] = newElement;
    }

    public void add(AnyType newVal) {
        AnyType old[] = this.theItem;

        this.theItem = (AnyType[]) new Object[size + 1];
        if (size != 0) {
            for (int i = 0; i < old.length; i++) {
                this.theItem[i] = old[i];
            }
        }
        this.theItem[this.size] = newVal;
        this.size++;
    }

    public static void main(String args[]) {
        DynamicArray<String> array = new DynamicArray<String>();
        array.add("1");
        array.add("2");
        array.add("3");
        array.add("a");
        array.add("b");
        array.add("c");
        for (int i = 0; i < array.size; i++) {
            System.out.println(array.get(i));
        }
    }
}
posted @ 2012-10-22 13:44  雲端之風  阅读(534)  评论(0编辑  收藏  举报