假设,我们需要匹配:
Anything or nothing, followed by a dot of which there may be 0 or 1, followed by the word "network", where N may come in lower case or upper case.
这在 Perl 中工作正常:
^.*(\.?)[Nn]etwork$
你会如何在 Java 中匹配它?我试过了
(.*)\\.?(N|n)etwork$
但是“blah.Network”不匹配
最佳答案
怎么了
^.*\\.?[Nn]etwork$
如在
boolean foundMatch = subjectString.matches("^.*\\.?[Nn]etwork$");
(圆点两边的括号是不必要的)。
关于java - 如何将这个正则表达式从 Perl 翻译成 Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7250045/