首页 > 代码库 > 正则表达式匹配大于50%的字符串

正则表达式匹配大于50%的字符串

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Frank
 */
public class REMatch {
    public static void main(String[] args) {
        String patt = "^[5]\\d{1}\\%|[5-9]\\d{1}\\%|\\d{3,}\\%$";
        Pattern r = Pattern.compile(patt);
        String line = "/dev/sda2 18G 5.0G 65G 50% /"; 
        Matcher m = r.matcher(line);
        int i = 0;
        while (m.find()) {
            // group(0)或group()将会返回整个匹配的字符串(完全匹配);group(i)则会返回与分组i匹配的字符
            // 这个例子只有一个分组
            System.out.println(patt + " matches \"" + m.group(0) + "\" in \"" + line + "\"");
            System.out.println("start:" + m.start() + " end:" + m.end());
        }
    }
}

 

正则表达式匹配大于50%的字符串