简体   繁体   English

Java String.matches(“ regex”)

[英]Java String.matches(“regex”)

I don't know regex very well. 我不太了解正则表达式。 I am trying to find strings that start with digits 2,3,5 or 7, are 1,3,7 or 9 throughout the middle, and end with 3 or 7. 我试图找到以2,3,5或7开头的字符串,中间是1,3,7或9,最后以3或7结尾的字符串。

My attempt was [2357][1379]*[37]. 我的尝试是[2357] [1379] * [37]。 It does not work. 这是行不通的。 I'd appreciate a correction. 我希望得到纠正。 Remember that this is meant for the Java String.matches() function. 请记住,这是针对Java String.matches()函数的。 Thanks in advance 提前致谢

   for (int s = 0; s < primes.size(); ++s) {
        String p = primes.get(s);

        if (!p.matches([REGEX GOES HERE])) {
            System.out.println(p);
            primes.remove(s);
        }
    }

The standard method of iterating over a collection you remove from in the loop is to iterate downwards that way removals don't affect the index of subsequent elements: 迭代从循环中删除的集合的标准方法是向下迭代,以使删除不会影响后续元素的索引:

for (int s = primes.size() - 1; s >= 0; s--) {
    String p = primes.get(s);
    if (!p.matches("[2357][1379]*[37]")) {
        System.out.println(p);
        primes.remove(s);
    }
}

No need now to worry about implications of removing elements. 现在无需担心删除元素的影响。

Hi I am not sure I am mistaken .. but i dont see anything wrong in your initial pattern for example 嗨,我不确定我弄错了..但是,例如,您在初始模式中没有看到任何错误

String aa = "[2357][1379]*[37]";
String bb = "2977313";

boolean matches = Pattern.matches(aa, bb); 
System.out.println("1) "+matches);

I started with no 2 and then ended with no 3 and added 1379 in between and it works as expected. 我以2开始,然后以3结束,并在两者之间添加了1379,它按预期工作。 Please correct me if i am wrong 如果我错了请指正我

The following code "works" just fine: 下面的代码“正常”就可以了:

    List<String> primes = new ArrayList<String>();
    primes.add("1");
    primes.add("2");
    primes.add("7");
    primes.add("23");
    primes.add("213");
    primes.add("243");
    primes.add("2113");
    primes.add("2193");
    for (int s = 0; s < primes.size(); ++s) {
        String p = primes.get(s);

        if (!p.matches("[2357][1379]*[37]")) {
            System.out.println(p);
            primes.remove(s);
        }
    }

It outputs: 它输出:

1
7
243

You may have expected it to output: 您可能期望它输出:

1
2
7
243

However, the primes.remove(s) is messing up your loop. 但是, primes.remove(s)弄乱了您的循环。 That can't really be the intent of your design. 那真的不是您设计的意图。 (But who knows?!) The following is one of many solutions to avoid messing up your loop: (但是谁知道?!)以下是避免弄乱循环的许多解决方案之一:

    for (String prime : new ArrayList<String>(primes)) {
        if (!prime.matches("[2357][1379]*[37]")) {
            System.out.println(prime);
            primes.remove(prime);
        }
    }

Your regex works well. 您的正则表达式效果很好。 However, remove shifts any subsequent elements to the left, so the String ordinarily at position s + 1 is moved to s , so the next element to check is at position s instead of s + 1 . 但是, remove将所有后续元素向左移动,因此通常将String在位置s + 1移至s ,因此要检查的下一个元素在位置s而不是s + 1 Fix: 固定:

for (int s = 0; s < primes.size();) {
    String p = primes.get(s);

    if (!p.matches("[2357][1379]*[37]")) {
        System.out.println(p);
        primes.remove(s);
    } else 
        ++s;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM