简体   繁体   English

Java模式REGEX不匹配的地方

[英]Java Pattern REGEX Where Not Matching

I'm trying to use the Java Pattern and Matcher to apply input checks. 我正在尝试使用Java模式和匹配器来应用输入检查。 I have it working in a really basic format which I am happy with so far. 到目前为止,我使用的是一种非常基本的格式,对此我很满意。 It applies a REGEX to an argument and then loops through the matching characters. 它将REGEX应用于参数,然后循环匹配字符。

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

public class RegexUtil {

   public static void main(String[] args) {

      String    argument;
      Pattern   pattern;
      Matcher   matcher;

      argument = "#a1^b2";
      pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s");
      matcher = pattern.matcher(argument);

      // find all matching characters
      while(matcher.find()) {
         System.out.println(matcher.group());
      }

   }

}

This is fine for extracting all the good characters, I get the output 这对于提取所有好的字符很好,我得到了输出

a
1
b
2

Now I wanted to know if it's possible to do the same for any characters that don't match the REGEX so I get the output 现在我想知道是否可以对与REGEX不匹配的任何字符执行相同的操作,因此我得到了输出

#
^

Or better yet loop through it and get TRUE or FALSE flags for each index of the argument 或者更好地遍历它,并为参数的每个索引获取TRUE或FALSE标志

false
true
true
false
true
true

I only know how to loop through with matcher.find(), any help would be greatly appreciated 我只知道如何通过matcher.find()循环,任何帮助将不胜感激

You may add a |(.) alternative to your pattern (to match any char but a line break char) and check if Group 1 matched upon each match. 您可以在模式中添加|(.)替代项(以匹配除换行符以外的任何字符),并检查每次匹配是否匹配组1。 If yes, output false , else, output true : 如果是,则输出false ,否则输出true

String argument = "#a1^b2";
Pattern pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s|(.)"); // or "[a-zA-Z0-9\\s]|(.)"
Matcher matcher = pattern.matcher(argument);

while(matcher.find()) {                           // find all matching characters
    System.out.println(matcher.group(1) == null);

See the Java demo , output: 参见Java演示 ,输出:

false
true
true
false
true
true

Note you do not need to use a Pattern.DOTALL here, because \\s in your "whitelist" part of the pattern matches line breaks. 请注意,您无需在此处使用Pattern.DOTALL ,因为该模式的“白名单”部分中的\\s与换行符匹配。

You have to iterate over each char of the String and check one by one : 您必须遍历String每个char ,并一个接一个地检查:

//for-each loop, shorter way
for (char c : argument.toCharArray()){
    System.out.println(pattern.matcher(c + "").matches());
}

or 要么

//classic for-i loop, with details
for (int i = 0; i < argument.length(); i++) {
    String charAtI = argument.charAt(i) + "";
    boolean doesMatch = pattern.matcher(charAtI).matches();
    System.out.println(doesMatch);
}

Also, when you don't require it, you can do declaration and give a value at same time : 另外,当您不需要它时,可以同时声明并提供值:

String argument = "#a1^b2";
Pattern pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s");

Why not simply removing all matching chars from your string, so you get only the non matching ones back: 为什么不简单地从字符串中删除所有匹配的字符,以便只返回不匹配的字符:

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

public class RegexUtil {

   public static void main(String[] args) {

      String    argument;
      Pattern   pattern;
      Matcher   matcher;

      argument = "#a1^b2";
      pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s");
      matcher = pattern.matcher(argument);

      // find all matching characters
      while(matcher.find()) {
         System.out.println(matcher.group());
         argument = argument.replace(matcher.group(), "");
      }

      System.out.println("argument: " + argument);

   }

}

Track positions, and for each match, print the characters between last match and current match. 跟踪位置,并为每个匹配项打印最后一个匹配项和当前匹配项之间的字符。

int pos = 0;
while (matcher.find()) {
    for (int i = pos; i < matcher.start(); i++) {
        System.out.println(argument.charAt(i));
    }
    pos = matcher.end();
}
// Print any trailing characters after last match.
for (int i = pos; i < argument.length(); i++) {
    System.out.println(argument.charAt(i));
}

One solution is 一种解决方案是

    String  argument;
    Pattern pattern;
    Matcher matcher;

    argument = "#a1^b2";
    List<String> charList = Arrays.asList(argument.split(""));
    pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s");
    matcher = pattern.matcher(argument);
    ArrayList<String> unmatchedCharList = new ArrayList<>();
    // find all matching
    while(matcher.find()) {
        unmatchedCharList.add(matcher.group());
    }
    for(String charr : charList)
    {
        System.out.println(unmatchedCharList.contains(charr ));
    }

Output 产量

false true true false true true 假真真假真真

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

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