我要用正则表达式匹配这样的文字:
====================
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2008-2009 Company, Inc. All rights reserved.
The contents of this file are subject to the terms of the Common Development
and Distribution License("CDDL") (the "License"). You may not use this file
except in compliance with the License.
You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL
See the License for the specific language governing permissions and limitations
under the License.
When distributing the Covered Code, include this CDDL Header Notice in each file
and include the License file at https://oss.oracle.com/licenses/CDDL.
If applicable, add the following below this CDDL Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"
====================
Copyright 2011-2013 Company. All rights reserved.
并且===========之间的部分是静态的永远不会改变,所以我可以以静态的方式搜索这个表达式,但它不是一个格式正确的正则表达式,我怎么能让所有正则表达式中=========== static之间的文本?
答案 0 :(得分:4)
如果您实际上无法编写代码,只能编写正则表达式,则可以使用\Q
和\E
关闭模式某个部分的所有元字符:
startOfRegex\Q============...\EendOfRegex
这样,\Q
和\E
之间的部分可以包含任意正则表达式元字符(如patrentheses,括号和星号以及反斜杠和诸如此类),而startOfRegex
和{{ 1}}可以是正常的和任意的正则表达式模式。
如果字符串包含endOfRegex
或以反斜杠结尾,则只会出现问题。在这种情况下,Tim Pietzker的\E
是唯一的通用方法。
答案 1 :(得分:3)
Netbeans使用Java,对吗?然后你可以使用
String regex = Pattern.quote(my_verbatim_string);
正确转义所有正则表达式元字符。
但很有可能,使用像(?s)={20}.*?={20}
这样的正则表达式查找====================
之间的所有文本,然后进行严格的相等比较会更好。