在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java正则表达式实现提取需要的字符并放入数组【ArrayList数组去重复功能】

2017-3-7 12:52| 发布者: zhangjf| 查看: 1364| 评论: 0

摘要: 本文实例讲述了java正则表达式实现提取需要的字符并放入数组。分享给大家供大家参考,具体如下: 这里演示Java正则表达式提取需要的字符并放入数组,即ArrayList数组去重复功能。 具体代码如下: package com.test.t ...

本文实例讲述了java正则表达式实现提取需要的字符并放入数组。分享给大家供大家参考,具体如下:

这里演示Java正则表达式提取需要的字符并放入数组,即ArrayList数组去重复功能。

具体代码如下:

  1. package com.test.tool;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.regex.*;
  5. public class MatchTest {
  6. public static void main(String[] args)
  7. {
  8. String regex = "[0-9]{5,12}";
  9. String input = "QQ120282458,QQ120282458 QQ125826";
  10. Pattern p = Pattern.compile(regex);
  11. Matcher m = p.matcher(input);
  12. ArrayList al=new ArrayList();
  13. while (m.find()) {
  14. al.add(m.group(0));
  15. }
  16. System.out.println("去除重复值前");
  17. for (int i=0;i<al.size();i++)
  18. {
  19. System.out.println(al.get(i).toString());
  20. }
  21. //去除重复值
  22. HashSet hs=new HashSet(al);
  23. al.clear();
  24. al.addAll(hs);
  25. System.out.println("去除重复值后");
  26. for (int i=0;i<al.size();i++)
  27. {
  28. System.out.println(al.get(i).toString());
  29. }
  30. }
  31. }
复制代码

输出结果为:

  1. 去除重复值前
  2. 120282458
  3. 120282458
  4. 125826
  5. 去除重复值后
  6. 125826
  7. 120282458
复制代码

改进版:弄成一个bean:

  1. package com.test.tool;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.regex.*;
  5. public class MatchTest {
  6. private String regex;
  7. private String input;
  8. private ArrayList al;
  9. public String getRegex() {
  10. return regex;
  11. }
  12. public void setRegex(String regex) {
  13. this.regex = regex;
  14. }
  15. public String getInput() {
  16. return input;
  17. }
  18. public void setInput(String input) {
  19. this.input = input;
  20. }
  21. public ArrayList getAl() {
  22. return al;
  23. }
  24. public void setAl(ArrayList al) {
  25. this.al = al;
  26. }
  27. public MatchTest(String regex,String input)
  28. {
  29. Pattern p=Pattern.compile(regex);
  30. Matcher m=p.matcher(input);
  31. ArrayList myal=new ArrayList();
  32. while (m.find())
  33. {
  34. myal.add(m.group());
  35. }
  36. HashSet hs=new HashSet(myal);
  37. myal.clear();
  38. myal.add(hs);
  39. this.setRegex(regex);
  40. this.setInput(input);
  41. this.setAl(myal);
  42. }
  43. public MatchTest(){}
  44. public static void main(String[] args){
  45. String regex1 = "[0-9]{5,12}";
  46. String input1="QQ120282458,QQ120282458 QQ125826";
  47. //String input1="QQ";
  48. MatchTest mt=new MatchTest(regex1,input1);
  49. for (int i=0;i<mt.getAl().size();i++)
  50. {
  51. System.out.println(mt.getAl().get(i).toString());
  52. }
  53. }
  54. }
复制代码

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述对大家java程序设计有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-3 12:32

Copyright 2015-2025 djqfx

返回顶部