简体   繁体   English

String的matches()方法的奇怪行为

[英]Strange behavior of String's matches() method

I encountered an interesting issue about String's matches(RegExp) method. 我遇到了一个关于String的匹配(RegExp)方法的有趣问题。

assertTrue("33CCFF".matches("[0-9A-Za-z]{6}"));
assertTrue("CC33FF".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3355".matches("[0-9A-Za-z]{6}"));
assertTrue("CC9955".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3366".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3965".matches("[0-9A-Za-z]{6}"));
assertTrue("CC1961".matches("[0-9A-Za-z]{6}"));
assertTrue("CC9999".matches("[0-9A-Za-z]{6}"));
assertTrue("СС3966".matches("[0-9A-Za-z]{6}")); // failing
assertTrue("СС9965".matches("[0-9A-Za-z]{6}")); // failing
assertTrue("СС9966".matches("[0-9A-Za-z]{6}")); // failing

The last 3 assertion is failing unexpectedly. 最后3个断言意外失败。 I couldn't find any reasons why this weird behavior is happening. 我找不到为什么会发生这种奇怪的行为的原因。 Do you have the same issue? 你有同样的问题吗? Do you have any ideas? 你有什么想法?

By the way, in case of being asked, my java version is the following. 顺便说一下,如果被问到,我的java版本如下。

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

The last three don't actually start with an ASCII "C" character. 最后三个实际上并不是以ASCII“C”字符开头。 They start with a non-ASCII character which looks like "C". 它们以非ASCII字符开头, 看起来像“C”。 That doesn't match anything in the [0-9A-Za-z] set, hence the pattern fails. 这与[0-9A-Za-z]集合中的任何内容都不匹配,因此模式失败。

(I found this out by copying and pasting the code into a text editor which doesn't handle non-ASCII characters terribly well - they came out as "?".) (我通过将代码复制并粘贴到文本编辑器中来发现这一点,该文本编辑器不能很好地处理非ASCII字符 - 它们以“?”形式出现。)

your "СС3966" (I'm cutting and pasting) are getting flagged as non UTF-8, which is why reg-ex isn't matching them. 你的“СС3966”(我正在剪切和粘贴)被标记为非UTF-8,这就是为什么reg-ex与它们不匹配的原因。 When I change your text and just type it myself it works as expected. 当我更改您的文本并自己键入它时,它按预期工作。 Not sure where you copied these values from, but that's your problem 不确定从哪里复制这些值,但那是你的问题

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

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