Java Regex Pattern Matching: Understanding '[[^dz]&&[^0-9]]5,)' and 'abcdef'
Code:
boolean matches = Pattern.matches("[[^dz]&&[^0-9]]5,", "abcdef");
System.out.println(matches ? "match" : "Mismatch");
Output: C - Mismatch
Explanation: The regular expression '[[^dz]&&[^0-9]]5,)' breaks down as follows:
- '[^dz]': Matches any character except 'd' or 'z'.
- '&&': Intersects the previous character class with the next one.
- '[^0-9]': Matches any character that is not a digit.
- '5': Matches the character '5' literally.
- ',': Matches the comma character literally.
- ')': Matches the closing parenthesis literally.
In essence, the regex searches for a string that starts with a character that is neither 'd' nor 'z' and is not a digit, followed by a '5' and a comma. The string 'abcdef' fails this test because it contains the letter 'd'. Therefore, the output is 'Mismatch'.
原文地址: https://www.cveoy.top/t/topic/ozZA 著作权归作者所有。请勿转载和采集!