问题列表 提出问题 发起投票 在线聊天 人员列表 关于本站

名称
密码

请记住我

名称


免费注册

  • 直销制度论坛
  • 中国调查网
  • 旅游论坛
  • 健康美丽网
  • 厦门测试调查网
  • 公司办公论坛
  • 技术论坛
  • PDCA论坛
  • 洛江论坛
  • 方案论坛
  • 安利论坛




  • 南狐软件版权所有
    问题: > JAVA正则表达式类的使用 :阅读192, 回复:0
    分类:技术文章
    JAVA正则表达式类的使用
    来自:来自: 福建省 泉州市 电信     时间:2010-4-11 17:01:17

    JAVA正则表达式类的使用

    文章分类:Java编程

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

    /**
    * @author ZhuYuanXi
    * 正则表达式的使用
    */
    public class Test {

    /**
    * 字符串匹配
    * @param expression 正则表达式字符串
    * @param text 要进行匹配的字符串
    */
    private static void matchingText(String expression, String text) {
    Pattern p = Pattern.compile(expression); // 正则表达式
    Matcher m = p.matcher(text); // 操作的字符串
    boolean b = m.matches();
    System.out.println(b);
    }

    /**
    * 字符串替换
    * @param expression 正则表达式字符串
    * @param text 要进行替换操作的字符串
    * @param str 要替换的字符串
    */
    private static void replaceText(String expression, String text, String str) {
    Pattern p = Pattern.compile(expression); // 正则表达式
    Matcher m = p.matcher(text); // 操作的字符串
    String s = m.replaceAll(str);
    System.out.println(s);
    }

    /**
    * 字符串查找
    * @param expression 正则表达式字符串
    * @param text 要进行查找操作的字符串
    * @param str 要查找的字符串
    */
    private static void findText(String expression, String text, String str) {
    Pattern p = Pattern.compile(expression); // 正则表达式
    Matcher m = p.matcher(text); // 操作的字符串
    StringBuffer sb = new StringBuffer();
    int i = 0;
    while (m.find()) {
    m.appendReplacement(sb, str);
    i++;
    }
    m.appendTail(sb);
    System.out.println(sb.toString());
    System.out.println(i);
    }

    /**
    * 字符串分割
    * @param expression 正则表达式字符串
    * @param text 要进行分割操作的字符串
    */
    private static void splitText(String expression, String text) {
    Pattern p = Pattern.compile(expression); // 正则表达式
    String[] a = p.split(text);
    for (int i = 0; i < a.length; i++) {
    System.out.println(a[i]);
    }
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    matchingText("^card_([_0-9a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?.shtml$", "card_1020000000.shtml");
    // 字符串匹配,这是不符合的
    matchingText("a*b", "baaaaab");
    // 字符串匹配,这是符合的
    matchingText("a*b", "aaaaab");
    // 字符串匹配,通用匹配
    matchingText("^([_0-9a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?", "aaaaab");

    // 字符串替换
    replaceText("ab", "aaaaab", "d");
    replaceText("a*b", "aaaaab", "d");
    replaceText("a*b", "caaaaab", "d");

    // 字符串查找
    findText("cat", "one cat two cats in the yard", "dog");
    findText("(fds){2,}", "dsa da fdsfds aaafdsafds aaf", "dog");

    // 字符串分割
    splitText("a+", "caaaaaat");
    splitText("a+", "c aa aaaa t");
    splitText(" +", "c aa aaaa t");
    splitText("\\+", "dsafasdfdsafsda+dsagfasdfa+sdafds");
    }

    }

    [上一条 用Javascript判断文本框空、长度、字符由汉字字母数字组成]
    [下一条 http://www.360doc.com/content/09/0817/15/219800_4990487.shtml]
    ,对此问题,您可以:

    8  

    投票
    8 添加您对此问题的注释(请注意换行)


         

    南狐科技版权所有
    感谢您的惠顾,如有任何建议和意见,请 联系版主2007.1.1