【问题标题】:how to filter values using Regex - Java如何使用正则表达式过滤值 - Java
【发布时间】:2016-08-19 16:17:48
【问题描述】:

我是使用正则表达式的新手;我有一些值要使用 Java 和 Regex 进行过滤。

  • 1000
  • 1200
  • 1300
  • 1310
  • 1320
  • 1330
  • 1400
  • 1410
  • 1420
  • 1430
  • 2000

结果应该是:

  1. 显示最后一个字符只有 0 的值。例如(1310、1320、1330、1410、1420、1430)
  2. 显示最后 2 个字符只有 00 的值。例如(1200、1300、1400)
  3. 显示最后 3 个字符只有 000 的值。 ex(1000, 2000)

【问题讨论】:

  • 那么您尝试了什么,为什么没有成功?
  • 我试过这个java代码来过滤0,但它不起作用。
  • 顺便说一句,看起来它可以在没有正则表达式的情况下完成,但只需 % 运算符。这是以防万一这是唯一的任务。
  • 你能解释一下吗
  • 如果你所有的输入都是数字并且你需要检查它们是否以零结尾,你可以这样做:1-to show the values that have only 0 as last character. ex(1410 , 1420,1430, 1310, 1320 , 1330)检查number % 10 == 0 && number % 100 != 02-to show the values that have only 00 as last character. ex(1400 , 1300,1200) 检查number % 100 == 0 && number % 1000 != 0

标签: java regex pattern-matching


【解决方案1】:
  String txt=key;

String re1=".*?";   // Non-greedy match on filler
String re2="(0)";   // Any Single Character 1

Pattern p = Pattern.compile(re1+re2,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{

   System.out.println(key+"  "+value);
}

【讨论】:

  • 键是一个包含这些值的数组
【解决方案2】:

假设您的列表是一个字符串数组,您可以简单地遍历列表并使用来自java.util.regx 的 Pattern 和 Matcher 测试每个字符串。

    ArrayList<String> numberList = new ArrayList();
    numberList.add("1230");
    numberList.add("1004");
    numberList.add("1000");

    Pattern pattern = Pattern.compile("0");
    for (int x = 0; x < numberList.size(); x++) {
         Matcher matcher = pattern.matcher(numberList.get(x));
         int count = 0;
         while (matcher.find()) {
              count++;
         }
         if (count == 1) {
             System.out.print(numberList.get(x) + " has one 0\n");
         }
         if (count == 2) {
             System.out.print(numberList.get(x) + " has two 0s\n");
         }
         if (count == 3) {
             System.out.print(numberList.get(x) + " has three 0s\n");
         }
    }

输出:

 1230 has one 0
 1004 has two 0s
 1000 has three 0s

【讨论】:

  • @J.Simpa 已编辑和测试。
【解决方案3】:

正则表达式解决方案有效 :) ;我还找到了另一个不使用正则表达式的解决方案:

if(key.endsWith("0") && !key.contains("00") && !key.contains("000")  )

感谢每一个人。

【讨论】:

  • 对于可能以“0”结尾但仍包含“00”的较长字符串(例如“10010”),这将无法正常工作。此外,“000”的测试是不必要的。如果key 不包含“00”,它肯定不包含“000”。
  • 是的,你是对的,我没有想到,但在我的情况下,我不希望像 10010 这样的数字,0 会一直在左边,而不是在中间.例如:1400,1420。 ..谢谢你。
【解决方案4】:

我会避免为此使用正则表达式。相反,我建议使用循环简单地计算尾随零的数量:

List<String> items = Arrays.asList("1000", "1200", "1300", "1310", ... );
for (String item : items) {
    int count = 0;
    for (int i = item.length() - 1; i >= 0 && item.charAt(i) == '0'; --i) {
        count++;
    }
    // item has "count" trailing zeroes
}

或者,我建议使用String#endsWith()。但是使用正则表达式或使用endsWith(),因为所有以“00”结尾的字符串也以“0”结尾,所以你必须对你的测试有点聪明。因此,例如,要允许“1420”而不是“1400”,您可以使用:

if (item.endsWith("0") && !item.endsWith("00")) {
    // item ends with exactly one 0
}

或者(我认为更好),您可以使用一系列if..else 条件,按尾随零长度的降序排列:

if (item.endsWith("000")) {
    // item ends with (at least) three zeroes
} else if (item.endsWith("00")) {
    // item ends with exactly two zeroes
} else if (item.endsWith("0")) {
    // item ends with exactly one zero
} else {
    // item does not end in a zero
}

如果您需要按尾随零的数量顺序输出项目,则需要将它们分类到 bin 中并在稍后的步骤中处理结果。像这样的:

List<String> one = new ArrayList<>();
List<String> two = new ArrayList<>();
List<String> three = new ArrayList<>();

for (String item : items) {
    if (item.endsWith("000")) {
        three.add(item);
    } else if (item.endsWith("00")) {
        two.add(item);
    } else if (item.endsWith("0")) {
        one.add(item);
    }
}

// now process the results:
System.out.print("Items with one trailing zero: ");
System.out.println(String.join(", ", one);
System.out.print("Items with two trailing zeroes: ");
System.out.println(String.join(", ", two);
System.out.print("Items with three or more trailing zeroes: ");
System.out.println(String.join(", ", three);

String#join() 方法是 Java 1.8 的新方法。如果您使用的是早期版本的 Java,则需要以不同的方式创建字符串表示。)

如果您仍然坚持使用正则表达式,您应该从匹配尾随零的Pattern 创建一个Matcher,如果找到匹配项,则检查匹配字符序列的长度:

Pattern p = new Pattern("0+$"); // match one or more trailing zeroes
for (String item : items) {
    Matcher m = p.matcher(item);
    if (m.find()) {
        int trailingZeroCount = m.group().length();
        // process accordingly
    }
}

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2018-06-23
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多